-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type aliases not being resolved for some functions types #30312
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
Comments
The two types are structurally equivalent, the only issue is with the quick info you see (as you already point out). Not sure what the heuristic is for when the type gets resolved or it is kept as is, but I have found the following let subject = {a:1,b:2,c:3,d:4}
type thisResolves = Pick<typeof subject, 'a' | 'b'>
let thisDoesnt = pick(subject, ['a', 'b']) // also expanded now
declare function pick<T, K extends keyof T>(obj: T, keys: K[]): Id<Pick<T, K>>
type Id<T extends object> = {} & { [P in keyof T]: T[P] } I use it mostly for debugging complex types, and I do not guarantee it will not cause problems in some corner casses, but it might be a useful workaround in some scenarios. |
So the difference here is that you're hovering over a type vs a value, and we are (for whatever reason) more eager in resolving through aliases in the type case than in the value case. If you wrote type A = typeof thisDoesnt; and hovered on Which of those is preferable is extremely situational and I'm not sure making a change here is guaranteed to be a net improvement over the status quo. |
Strange, the Are there any current proposals to "suggest" to the TS compiler which aliases to collapse and which to retain? |
I have a problem related to this which is making some types in a project practially unreadable. I'm using the following type export type Remap<
A extends Record<any, any>,
B extends Record<any, keyof A>
> = { [K in keyof B]: A[B[K]] }; When type Big = { a: number, b: number, c: string, d: number, e: number }
function f<A extends Record<string, keyof Big>(a: A): Remap<Big, A> {
return 0 as any;
}
const a = f({q: "c"}); If I hover over I understand that unfolding type definitions isn't always what one wants. But, I have a strong feeling that there is a better approach than what is currently done. Maybe just always select the shortest (measured in characters) option between fully computed and not at all computed? |
TypeScript Version: 3.4.0-dev.20190310
Search Terms:
Resolve / flatten / simplify type aliases for function calls
resolve type aliases
Code
Expected behavior:
The Quick Info type of
thisDoesnt
resolves to{ a: number, b: number}
Actual behavior:
The Quick Info type of
thisDoesnt
resolves toPick<{ a: number, b: number, c: number, d: number}, 'a' | 'b'>
Playground Link: here
Related Issues:
I've seen in other issues that type aliases are eagerly resolved (e.g. #13095 (comment) and #16798 (comment)). This is a case where that behavior is useful, since reading through a lot of
Pick<Foo<Bar<...
in VS Code makes it difficult to figure out the true source of a type error.The text was updated successfully, but these errors were encountered: