microsoft / microsoft/TypeScript

"paths" option should allow untyped entries

Ouverte
#35,772 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

Search Terms

paths option untyped declare module

Suggestion

The existing "paths" option allows you to control the types used for bare specifiers in your source code, by mapping them as keys to a list of candidate target specifiers.

// tsconfig.json
{
  "paths": {
    // <bare-specifier>: [ <candidate1>, <candidate2> ]
    "foo": [ "external-foo", "fallback-foo" ]
        // if "external-foo" is not found, "fallback-foo" will be attempted, otherwise error
  }
}

I suggest we allow the final element in the array of candidates to be the empty string "". If hit, it would mean "This module exists but is untyped."

Use Cases

Our build system uses "paths" to implement a searching loader for typed dependencies. We know all the acceptable source specifiers ahead-of-time, and so create one property for each - this ensures precise auto-completions for external dependencies in VS Code. We do not know if the target specifiers will exist on disk or not, e.g. the user may not yet have populated the disk via "npm install".

Strict: Some users want to be strict and see errors if no match was found. This behavior comes for free from "paths" today.

Sloppy: Sometimes users want to be sloppy and have the source specifier be typed as any if no match was found. This behavior is not possible today because there is no way to express "this modules exists but is untyped" in "paths" today.

Workaround

We have not discovered any alternative that allows sloppy behavior to be implemented.

The closest workaround is to write declare module "foo" in an ambient declaration file. However this does not work because it clobbers (takes precedence over) the same entry in "paths". Our desire is for untyped behavior to be the last resort, not the first choice.

Examples

Current behaviour: This works today and is fine for implementing strict behavior.

// tsconfig.json
{
  "paths": {
    "foo": [ "external-foo", "fallback-foo" ]
        // assume "external-foo" and "fallback-foo" do not exist on disk
  }
}

// index.ts
import aDefaultImport, { aNamedImport } from "foo";  // Error(2307): Cannot find module

Proposed behaviour: Here's how we could achieve sloppy behavior.

// tsconfig.json
{
  "paths": {
    "foo": [ "external-foo", "fallback-foo", "" ]
        // assume "external-foo" and "fallback-foo" do not exist on disk
  }
}

// index.ts
import aDefaultImport, { aNamedImport } from "foo";  // no error

The usage of "foo" would result in no compile-time error.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Aucun fichier, test ou point d’entrée n’est nommé. Commencez par localiser la gestion par le compilateur des chemins tsconfig.json et de la résolution des modules, puis suivez comment un candidat vide final pourrait représenter un module non typé. C’est terminé lorsque la configuration proposée compile les imports sans erreur lorsque les candidats de chemin précédents sont absents.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.