-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Conditional type has different result when inline generics #23656
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
Labels
Duplicate
An existing issue was already created
Comments
Using parentheses does not help: export type ActionData<TAction extends Action<TActionType>, TActionType extends string> =
Pick<TAction, ((keyof TAction) extends (keyof Action<TActionType>) ? never : (keyof TAction))>;
export interface Action<TActionType extends string> {
readonly type: TActionType;
}
interface TestAction extends Action<'test'> {
readonly type: 'test';
readonly testText: string;
readonly testNum: number;
readonly testBoolean: boolean;
}
let data: ActionData<TestAction, 'test'> = {
testText: 'd',
testNum: 3,
testBoolean: true
}; |
Duplicate of #23046. Please see explanation in #23022 (comment) |
Oooh, I see. This is quite a tricky thing. Thanks for the answer. 😄 |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
TypeScript Version:
2.8.3
Environment:
Visual Studio Code 1.22.2
Search Terms:
Conditional types
Code
Expected behavior:
I want to remove the 'type' property of the action type to express the data part. If you look at the working example, the diff expression is defined in a separate type definition. In this case the 'type' property is not part of the ActionData type.
Actual behavior:
I would expect that when I inline the Diff type expression into the ActionData type definition, the result is the same. That is not the case though. In the second case 'type' property is part of ActionData type, so the code does not compile, because the compiler expects a 'type' property to be there in the object literal for the "data" variable.
Playground Link:
Compiling code
https://www.typescriptlang.org/play/#src=type%20Diff%3CT%2C%20U%3E%20%3D%20T%20extends%20U%20%3F%20never%20%3A%20T%3B%0D%0Aexport%20type%20ActionData%3CTAction%20extends%20Action%3CTActionType%3E%2C%20TActionType%20extends%20string%3E%20%3D%20%0D%0A%20%20%20%20Pick%3CTAction%2C%20Diff%3Ckeyof%20TAction%2C%20keyof%20Action%3CTActionType%3E%3E%3E%3B%0D%0A%0D%0Aexport%20interface%20Action%3CTActionType%20extends%20string%3E%20%7B%0D%0A%20%20%20%20readonly%20type%3A%20TActionType%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20TestAction%20extends%20Action%3C'test'%3E%20%7B%0D%0A%20%20%20%20readonly%20type%3A%20'test'%3B%0D%0A%20%20%20%20readonly%20testText%3A%20string%3B%0D%0A%20%20%20%20readonly%20testNum%3A%20number%3B%0D%0A%20%20%20%20readonly%20testBoolean%3A%20boolean%3B%0D%0A%7D%0D%0A%0D%0Alet%20data%3A%20ActionData%3CTestAction%2C%20'test'%3E%20%3D%20%7B%0D%0A%20%20%20%20testText%3A%20'd'%2C%0D%0A%20%20%20%20testNum%3A%203%2C%0D%0A%20%20%20%20testBoolean%3A%20true%0D%0A%7D%3B
Non compiling code
https://www.typescriptlang.org/play/#src=export%20type%20ActionData%3CTAction%20extends%20Action%3CTActionType%3E%2C%20TActionType%20extends%20string%3E%20%3D%20%0D%0A%20%20%20%20Pick%3CTAction%2C%20keyof%20TAction%20extends%20keyof%20Action%3CTActionType%3E%20%3F%20never%20%3A%20keyof%20TAction%3E%3B%0D%0A%0D%0Aexport%20interface%20Action%3CTActionType%20extends%20string%3E%20%7B%0D%0A%20%20%20%20readonly%20type%3A%20TActionType%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20TestAction%20extends%20Action%3C'test'%3E%20%7B%0D%0A%20%20%20%20readonly%20type%3A%20'test'%3B%0D%0A%20%20%20%20readonly%20testText%3A%20string%3B%0D%0A%20%20%20%20readonly%20testNum%3A%20number%3B%0D%0A%20%20%20%20readonly%20testBoolean%3A%20boolean%3B%0D%0A%7D%0D%0A%0D%0Alet%20data%3A%20ActionData%3CTestAction%2C%20'test'%3E%20%3D%20%7B%0D%0A%20%20%20%20testText%3A%20'd'%2C%0D%0A%20%20%20%20testNum%3A%203%2C%0D%0A%20%20%20%20testBoolean%3A%20true%0D%0A%7D%3B
The text was updated successfully, but these errors were encountered: