Skip to content

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

Closed
elektromodulator opened this issue Apr 24, 2018 · 3 comments
Closed

Conditional type has different result when inline generics #23656

elektromodulator opened this issue Apr 24, 2018 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@elektromodulator
Copy link

elektromodulator commented Apr 24, 2018

TypeScript Version:
2.8.3
Environment:
Visual Studio Code 1.22.2
Search Terms:
Conditional types
Code

// This example compiles
type Diff<T, U> = T extends U ? never : T;
type ActionData<TAction extends Action<TActionType>, TActionType extends string> = 
    Pick<TAction, Diff<keyof TAction, keyof Action<TActionType>>>;

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
};
// This example does not compile, ActionData has 'type' property
type ActionData<TAction extends Action<TActionType>, TActionType extends string> = 
    Pick<TAction, keyof TAction extends keyof Action<TActionType> ? never : keyof TAction>;

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
};

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

@elektromodulator
Copy link
Author

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
};

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%20((keyof%20TAction)%20extends%20(keyof%20Action%3CTActionType%3E)%20%3F%20never%20%3A%20(keyof%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

@mhegazy
Copy link
Contributor

mhegazy commented Apr 24, 2018

@mhegazy mhegazy added the Duplicate An existing issue was already created label Apr 24, 2018
@elektromodulator
Copy link
Author

elektromodulator commented Apr 25, 2018

Oooh, I see. This is quite a tricky thing. Thanks for the answer. 😄

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants