-
Notifications
You must be signed in to change notification settings - Fork 399
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
Utility type KnownKeys<T>
broken on TypeScript 4.3
#951
Comments
Hi @lokshunhung, thank you very much for taking the time to write this clear and thorough bug report! I've confirmed this is reproducible in my environment too. |
I've looked into the implementation of type KnownKeys<T> = {
[K in keyof T]: string extends K ? never :
number extends K ? never :
K;
} extends { [_ in keyof T]: infer U }
? U
: never; The first clause reads as "Given a type I think this implementation can be further simplified to the following for better readability: type KnownKeys<T> = {
[K in keyof T]: string extends K ? never :
number extends K ? never :
K;
}[keyof T] ...which essentially does the same thing, doing the |
Unfortunately after refactoring Digging further in the codebase, I found that A solution to the current issue that I could think of at the moment, is to completely remove Let me know your thoughts :) |
@lokshunhung Thanks for sharing your insights! Yes, I was also thinking that, as a last option, we may have to stop using |
@lokshunhung Reviewed it and it looks great to me! You hero, thanks so much 👏 |
@seratch I think I found a solution to use a generic utility type. TLDR: Let's keep the current solution as-is, because the trade-off from a generic solution is not worth it. This can be achieved using key remapping in mapped types to check the types of keys inside index signature with conditional types, instead of using a conditional type as a object property. This solves the issue of TS4.3 seems to evaluate the types of keys too early (I believe) and causes the original utility type to fail. The final code can be refactored to: export type SayArguments = Omit<
OmitIndexSignature<ChatPostMessageArguments>,
'channel'
> & { /* ... */ };
export type RespondArguments = Omit<
OmitIndexSignature<ChatPostMessageArguments>,
'channel' | 'text'
> & { /* ... */ }; And the utility type looks like this: type OmitIndexSignature<T> = {
[K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
}; This reads as "Given a type However, the downside is that key remapping is a feature that is only available after TS4.1, which means causing problems to users using an older version of TypeScript if this solution is used. So I guess it's best in the interest of the majority of users to use the more verbose way and "pick" all the properties of |
@lokshunhung It's great to know that there is a new way to achieve the same but I agree that this project won't go with the approach considering the supported TS versions. Thanks a lot for sharing this insightful knowledge! |
@lokshunhung thats the only implementation that worked for me, thank you :) |
Description
The following code no longer compiles after upgrading from TypeScript 4.2.4 to 4.3.2
(Click here to expand)
I have narrowed down the problem to the utility type
KnownKeys<T>
in/src/types/helpers.ts
.KnownKeys<T>
failing to resolve the keys has causedRespondArguments
in/src/types/utilities.ts
to omit the property "blocks" by callingPick
withnever
as the 2nd type argument:...which in turn causes
RespondFn
to report an error when "blocks" is included inrespond({ /* ... */ })
What type of issue is this? (place an
x
in one of the[ ]
)Requirements (place an
x
in each of the[ ]
)Bug Report
Filling out the following details about bugs will help us solve your issue sooner.
Reproducible in:
package version: 3.3.0
node version: 15.12.0
OS version(s): OSX 11.3.1
Steps to reproduce:
(See description)
Also,
/types-tests/utilities.test-d.ts
should show an error after upgrading to TypeScript 4.3 (See attachment)Expected result:
KnownKeys<T>
should work properly in TypeScript 4.3.Maybe add a test case to cover
type Result = KnownKeys<Foo>;
should not infer typenever
.Actual result:
KnownKeys<T>
infersnever
in TypeScript 4.3.Attachments:
The text was updated successfully, but these errors were encountered: