microsoft / microsoft/TypeScript
Computed import types
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
Suggestion
🔍 Search Terms
import label:feature-request
✅ Viability 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
Add ability to typeof import() a string literal type
📃 Motivating Example
declare module 'test' {
export const test: number;
}
type GetExport<T extends string> = typeof import(T)
type Test = GetExport<'test'> // typeof import('test') ≈ {test: number}
💻 Use Cases
babel, eslint and webpack use package names in their API, dynamically require them and proxy data into their interface. Generally, API that looks like
doSomething({
package: 'package-name',
options: {
optionForThisPackage: 'value',
},
});
could greatly improve on typing with this.
Also it's often the case with dynamic imports that resulting Promise gets passed through several other methods (like React.lazy or loadable from @loadable/component). Currently the only way to get good types there is to have these duplicated at every import() site. It would be possible to assign some types to wrapper function in this case too.
type LazyRegistry<Packages extends Record<string, string>> = {
[Name in keyof Packages]: Promise<(typeof import(Name))[Packages[Name]]>
}
const lazyRegistry = <Packages extends Record<string, string>>(packages: Packages): LazyRegistry<Packages> => {
return Object.fromEntries(
Object.entries(packages).map(([name, importName]) => {
const lazy = React.lazy(async () => {
return (await import(name))[importName];
});
return [key, lazy] as const;
}),
) as LazyRegistry<Packages>;
};
type LoadableRegistry<Packages extends Record<string, string>> = {
[Name in keyof Packages]: AsLoadableComponent<typeof import(Packages[Name])>
}
const loadableRegistry = <Packages extends Record<string, string>>(packages: Packages) => {
return Object.fromEntries(
Object.entries(packages).map(([name, importName]) => {
return [key, loadable(() => import(importName))] as const;
}),
) as LoadableRegistry<Packages>;
};
const DynamicComponentRegistry = someRegistry({
Foo: 'Foo',
Bar: 'Bar',
});
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginnen Sie mit der Überprüfung der bestehenden Behandlung von typeof import('literal') und der Beispiele für Type Queries im Issue. Definieren Sie den Abschluss so, dass ein generischer String-Literal-Typ in typeof import(T) zulässig ist und gleichzeitig die demonstrierten Anwendungsfälle für die Typisierung von Modulen und dynamischen Imports erhalten bleiben; der Payload nennt keine Repository-Dateien oder Tests.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 30/100