2

Talk is cheap,show the code! By the way, the version of ts I am testing is 4.6.4

type ITypeA = ((args: { A: any }) => any) | ((args: { B: any }) => any);

type Test<T> = T extends (args: infer A) => any ? A : never;

// type Result1 = {
//     A: any;
// } | {
//     B: any;
// }
type Result1 = Test<ITypeA>;

// type Result2 = {
//     A: any;
// } & {
//     B: any;
// }
type Result2 = ITypeA extends (args: infer A) => any ? A : never;

Result1 may use 'distributive conditional types' rule in ts, so type Result1 = { A: any;} | { B: any;}.

My question is why does Result2 not apply this rule? Is there any difference between them?

Playground Link

1 Answer 1

1

Please see docs

When conditional types act on a generic type, they become distributive when given a union type. For example, take the following:

There is a requirement, you need act on generic.

Result2 acts on known types, there is no generic, whereas Result1 uses Test which in turn uses generic T

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!! I understand! It must act on generic!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.