-
Notifications
You must be signed in to change notification settings - Fork 12.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
String literal in typeof import in typescript 4.1.0-beta. #41550
Comments
My understanding is that in order to type-check the program, all program files need to be loaded to analyze each scope and make sense of the world. In order to do that, all import strings need to be known ahead of time. As a result, there unfortunately can't be a cycle between the type-checking and program construction phases. |
If that's true how would .d.ts files work currently? because they are not referenced by any import statement but are implicitly in scope ? (Module augmentation) All types MUST be known at compile time, otherwise TSC wouldn't work. Because all types are known at compile time surely we can just resolve the generics in import statements and load in those files as-well. Hope i'm understanding TSC properly here. |
Program construction proceeds as follows:
There is no going backwards in this algorithm - once you bring in a new file, you'd have to completely restart the typechecking process since you have no real way of knowing whether type data in that file invalidated previous typechecking outputs. |
@RyanCavanaugh @DanielRosenwasser Types that can represent your file system. Before: const response = await api<typeof import("/api/user")>("/api/user") After: const response = await api.user(1); // implementation
const api = new Proxy<FileSystem<"../../../api">>(...); Semantics of 'FileSystem' (name yet to be finalized). FileSystem traverses the files in the specified folder: Because the end result of this type will just be a object it can easily be used in conjunction with existing 'Mapped' types etc. More use cases: // (note: requires augmentation of JSX types);
<img src={"/static/dolphin.png"}/> // error dolphin.png isn't loaded in the file system. 2: Support for string based routing in applications and protection against routing refactors at a type-level. // (note: requires augmentation of JSX types);
<a href={"/home/name"}/> 3: Statically typed readFileSync for people using it to access their file system const readFileSystem = (path: keyof FileSystem<".">): string=> fs.readFileSync(path); 4: Every use case where previously people had alot of types or classes floating around and then had a single place where they imported all those types/classes into a union just to support typescript can now just use a folder to store those things in conjunction with // people using ORM's can avoid having a 'single' source where all their classes are imported in favor of just using a folder.
await database.user(123);
// implementation
class SomeORMClass {
getUsers() {
return [];
}
}
// Replaced via FileSystem helper type.
type FileSystem = {
users: {default: SomeORMClass} // representation of import("..") that has a default class export
}
type ORM = {
[K in keyof FileSystem]: FileSystem[K]["default"]
}
declare const database: ORM ;
database.users.getUsers(); Further thinking but not neccessary:
Precedence: When people look at the implementation in the standard library they will notice FileSystem doesn't have a implementation in the same way you can "peek definition" for types like What would be your thoughts on this? In my mind this would be a complete game changer and solves so many more use cases not listed here: FileSystem based Redux Action types. |
Think this has legs so created a separate feature request, feel free to shoot it down there. |
I basically just want to be able to imbue a function with the same power that a I'd love to be able to do something like |
TypeScript Version: 4.1.0-beta
Code
Expected behavior:
No error.
Actual behavior:
Error
Playground Link:
The reason this is desirable is because i have a API of "typed" endpoints and i often do this.
with generics + new string literal types in 4.1 i was hoping to achieve this.
The text was updated successfully, but these errors were encountered: