microsoft / microsoft/TypeScript

Narrowing string down to a module specifier

Ouverte
#62,703 3 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.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

🔍 Search Terms

string, narrow, module, import, specifier

✅ Viability Checklist
⭐ Suggestion

Being able to declare a string as resolvable (a module specifier), without having to use an actual import or typeof import(specifier).

📃 Motivating Example

The following shows what this issue is about, using a hypothetical Resolvable:

// Would be nice if one could resolve/dereference these anywhere, not only inside `import`.
// Get intellisense and type checker support for string and identifier here:
const foo: Resolvable = "foo";
const bar: Resolvable = "bar";

// and here:
const b: Record<string, Resolvable> = {
  [foo],
  baz: bar,
};

// Nuh-uh: "Hello World!" is not resolvable;
b[foo] = "Hello World!";

// and anywhere;
await import(feelingBazyToday ? b["baz"] : b[foo]);

The following shows a workaround, with clearly different DX and architecture.

const foo = "foo";
const bar = "bar";

// Is the following pattern beneficial or does it hurt?
// To me it looks like useless wrappers and repetition.
// It's also harder to describe the `typeof a`, although
// one may rely on inference in some situations.
const a: Record<string, ((...x: any[]) => GoodLuck)> = {
  foo: () => import(foo),
  baz: () => import(bar),
};

// May or may not be ok, but it's a different problem.
a[foo] = console.log;

await (feelingBazyToday ? a["baz"] : a[foo])();
💻 Use Cases
  1. What do you want to use this for?
  2. What shortcomings exist with current approaches?
  3. What workarounds are you using in the meantime?

  1. Improving DX in IDEs without compromising on runtime code. In particular it would help prevent errors everywhere a (future) module specifier string is decoupled from the dynamic import.

  2. Dynamic imports have runtime effects. It's currently not possible to dereference a specifier before use (anywhere it might appear).

  3. Either: Writing typeof import(foo) (and deleting it again), when one wants to know (or navigate to) what foo would resolve to. This partially solves the problem, by effectively validating the string, but that information is lost. Or: A different architecture similar to the workaround in the motivating example. Also note, that the described feature can't be implemented in TS user land with things like utility types or type discrimination; so it's not merely sugar for existing functionality. Every workaround will lack a key aspect of the described feature.

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 source, test ou point d’entrée n’est nommé. Commencez par examiner la suggestion et les exemples motivants, puis déterminez comment un type module-specifier résoluble permettrait la vérification et la navigation sans modifier le JavaScript émis ; le travail serait considéré comme terminé lorsqu’un design arrêté ainsi que le comportement correspondant de type-checker et de l’IDE seraient définis.

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é
À clarifier
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

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