Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19832,7 +19832,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const typeParamMapper = combineTypeMappers((newType as ConditionalType).mapper, newMapper);
const typeArguments = map(newRoot.outerTypeParameters, t => getMappedType(t, typeParamMapper));
const newRootMapper = createTypeMapper(newRoot.outerTypeParameters, typeArguments);
const newCheckType = newRoot.isDistributive ? getMappedType(newRoot.checkType, newRootMapper) : undefined;
let newCheckType = newRoot.isDistributive ? getMappedType(newRoot.checkType, newRootMapper) : undefined;
if (newCheckType && isNoInferType(newCheckType)) {
newCheckType = (newCheckType as SubstitutionType).baseType;
}
if (!newCheckType || newCheckType === newRoot.checkType || !(newCheckType.flags & (TypeFlags.Union | TypeFlags.Never))) {
root = newRoot;
mapper = newRootMapper;
Expand Down Expand Up @@ -20928,7 +20931,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (!result) {
const newMapper = createTypeMapper(root.outerTypeParameters, typeArguments);
const checkType = root.checkType;
const distributionType = root.isDistributive ? getReducedType(getMappedType(checkType, newMapper)) : undefined;
let distributionType = root.isDistributive ? getReducedType(getMappedType(checkType, newMapper)) : undefined;
if (distributionType && isNoInferType(distributionType)) {
distributionType = (distributionType as SubstitutionType).baseType;
}
// Distributive conditional types are distributed over union types. For example, when the
// distributive conditional type T extends U ? X : Y is instantiated with A | B for T, the
// result is (A extends U ? X : Y) | (B extends U ? X : Y).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
//// [tests/cases/conformance/types/typeRelationships/typeInference/noInferVsDistributiveConditionalType1.ts] ////

=== noInferVsDistributiveConditionalType1.ts ===
// https://github.com/microsoft/TypeScript/issues/61076

type FooEvent = { type: "FOO" };
>FooEvent : Symbol(FooEvent, Decl(noInferVsDistributiveConditionalType1.ts, 0, 0))
>type : Symbol(type, Decl(noInferVsDistributiveConditionalType1.ts, 2, 17))

type BarEvent = { type: "BAR" };
>BarEvent : Symbol(BarEvent, Decl(noInferVsDistributiveConditionalType1.ts, 2, 32))
>type : Symbol(type, Decl(noInferVsDistributiveConditionalType1.ts, 3, 17))

type Input = FooEvent | BarEvent;
>Input : Symbol(Input, Decl(noInferVsDistributiveConditionalType1.ts, 3, 32))
>FooEvent : Symbol(FooEvent, Decl(noInferVsDistributiveConditionalType1.ts, 0, 0))
>BarEvent : Symbol(BarEvent, Decl(noInferVsDistributiveConditionalType1.ts, 2, 32))

type Result = Extract<NoInfer<Input>, FooEvent>;
>Result : Symbol(Result, Decl(noInferVsDistributiveConditionalType1.ts, 5, 33))
>Extract : Symbol(Extract, Decl(lib.es5.d.ts, --, --))
>NoInfer : Symbol(NoInfer, Decl(lib.es5.d.ts, --, --))
>Input : Symbol(Input, Decl(noInferVsDistributiveConditionalType1.ts, 3, 32))
>FooEvent : Symbol(FooEvent, Decl(noInferVsDistributiveConditionalType1.ts, 0, 0))

type EventObject = {
>EventObject : Symbol(EventObject, Decl(noInferVsDistributiveConditionalType1.ts, 6, 48))

type: string;
>type : Symbol(type, Decl(noInferVsDistributiveConditionalType1.ts, 8, 20))

};

type ActionFunction<
>ActionFunction : Symbol(ActionFunction, Decl(noInferVsDistributiveConditionalType1.ts, 10, 2))

TExpressionEvent extends EventObject,
>TExpressionEvent : Symbol(TExpressionEvent, Decl(noInferVsDistributiveConditionalType1.ts, 12, 20))
>EventObject : Symbol(EventObject, Decl(noInferVsDistributiveConditionalType1.ts, 6, 48))

TEvent extends EventObject,
>TEvent : Symbol(TEvent, Decl(noInferVsDistributiveConditionalType1.ts, 13, 39))
>EventObject : Symbol(EventObject, Decl(noInferVsDistributiveConditionalType1.ts, 6, 48))

> = {
(args: { event: TExpressionEvent }): void;
>args : Symbol(args, Decl(noInferVsDistributiveConditionalType1.ts, 16, 3))
>event : Symbol(event, Decl(noInferVsDistributiveConditionalType1.ts, 16, 10))
>TExpressionEvent : Symbol(TExpressionEvent, Decl(noInferVsDistributiveConditionalType1.ts, 12, 20))

_out_TEvent?: TEvent;
>_out_TEvent : Symbol(_out_TEvent, Decl(noInferVsDistributiveConditionalType1.ts, 16, 44))
>TEvent : Symbol(TEvent, Decl(noInferVsDistributiveConditionalType1.ts, 13, 39))

};

type TransitionsConfig<TEvent extends EventObject> = {
>TransitionsConfig : Symbol(TransitionsConfig, Decl(noInferVsDistributiveConditionalType1.ts, 18, 2))
>TEvent : Symbol(TEvent, Decl(noInferVsDistributiveConditionalType1.ts, 20, 23))
>EventObject : Symbol(EventObject, Decl(noInferVsDistributiveConditionalType1.ts, 6, 48))

[K in TEvent["type"]]?: {
>K : Symbol(K, Decl(noInferVsDistributiveConditionalType1.ts, 21, 3))
>TEvent : Symbol(TEvent, Decl(noInferVsDistributiveConditionalType1.ts, 20, 23))

actions?: ActionFunction<Extract<TEvent, { type: K }>, TEvent>;
>actions : Symbol(actions, Decl(noInferVsDistributiveConditionalType1.ts, 21, 27))
>ActionFunction : Symbol(ActionFunction, Decl(noInferVsDistributiveConditionalType1.ts, 10, 2))
>Extract : Symbol(Extract, Decl(lib.es5.d.ts, --, --))
>TEvent : Symbol(TEvent, Decl(noInferVsDistributiveConditionalType1.ts, 20, 23))
>type : Symbol(type, Decl(noInferVsDistributiveConditionalType1.ts, 22, 46))
>K : Symbol(K, Decl(noInferVsDistributiveConditionalType1.ts, 21, 3))
>TEvent : Symbol(TEvent, Decl(noInferVsDistributiveConditionalType1.ts, 20, 23))

};
};

declare function createMachine<TEvent extends EventObject>(config: {
>createMachine : Symbol(createMachine, Decl(noInferVsDistributiveConditionalType1.ts, 24, 2))
>TEvent : Symbol(TEvent, Decl(noInferVsDistributiveConditionalType1.ts, 26, 31))
>EventObject : Symbol(EventObject, Decl(noInferVsDistributiveConditionalType1.ts, 6, 48))
>config : Symbol(config, Decl(noInferVsDistributiveConditionalType1.ts, 26, 59))

types?: {
>types : Symbol(types, Decl(noInferVsDistributiveConditionalType1.ts, 26, 68))

events?: TEvent;
>events : Symbol(events, Decl(noInferVsDistributiveConditionalType1.ts, 27, 11))
>TEvent : Symbol(TEvent, Decl(noInferVsDistributiveConditionalType1.ts, 26, 31))

};
on?: TransitionsConfig<NoInfer<TEvent>>;
>on : Symbol(on, Decl(noInferVsDistributiveConditionalType1.ts, 29, 4))
>TransitionsConfig : Symbol(TransitionsConfig, Decl(noInferVsDistributiveConditionalType1.ts, 18, 2))
>NoInfer : Symbol(NoInfer, Decl(lib.es5.d.ts, --, --))
>TEvent : Symbol(TEvent, Decl(noInferVsDistributiveConditionalType1.ts, 26, 31))

}): void;

createMachine({
>createMachine : Symbol(createMachine, Decl(noInferVsDistributiveConditionalType1.ts, 24, 2))

types: {
>types : Symbol(types, Decl(noInferVsDistributiveConditionalType1.ts, 33, 15))

events: {} as Input,
>events : Symbol(events, Decl(noInferVsDistributiveConditionalType1.ts, 34, 10))
>Input : Symbol(Input, Decl(noInferVsDistributiveConditionalType1.ts, 3, 32))

},
on: {
>on : Symbol(on, Decl(noInferVsDistributiveConditionalType1.ts, 36, 4))

FOO: {
>FOO : Symbol(FOO, Decl(noInferVsDistributiveConditionalType1.ts, 37, 7))

actions: ({ event }) => {
>actions : Symbol(actions, Decl(noInferVsDistributiveConditionalType1.ts, 38, 10))
>event : Symbol(event, Decl(noInferVsDistributiveConditionalType1.ts, 39, 17))

event; // { type: "FOO"; }
>event : Symbol(event, Decl(noInferVsDistributiveConditionalType1.ts, 39, 17))

},
},
},
});

141 changes: 141 additions & 0 deletions tests/baselines/reference/noInferVsDistributiveConditionalType1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//// [tests/cases/conformance/types/typeRelationships/typeInference/noInferVsDistributiveConditionalType1.ts] ////

=== noInferVsDistributiveConditionalType1.ts ===
// https://github.com/microsoft/TypeScript/issues/61076

type FooEvent = { type: "FOO" };
>FooEvent : FooEvent
> : ^^^^^^^^
>type : "FOO"
> : ^^^^^

type BarEvent = { type: "BAR" };
>BarEvent : BarEvent
> : ^^^^^^^^
>type : "BAR"
> : ^^^^^

type Input = FooEvent | BarEvent;
>Input : Input
> : ^^^^^

type Result = Extract<NoInfer<Input>, FooEvent>;
>Result : FooEvent
> : ^^^^^^^^

type EventObject = {
>EventObject : EventObject
> : ^^^^^^^^^^^

type: string;
>type : string
> : ^^^^^^

};

type ActionFunction<
>ActionFunction : ActionFunction<TExpressionEvent, TEvent>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

TExpressionEvent extends EventObject,
TEvent extends EventObject,
> = {
(args: { event: TExpressionEvent }): void;
>args : { event: TExpressionEvent; }
> : ^^^^^^^^^ ^^^
>event : TExpressionEvent
> : ^^^^^^^^^^^^^^^^

_out_TEvent?: TEvent;
>_out_TEvent : TEvent | undefined
> : ^^^^^^^^^^^^^^^^^^

};

type TransitionsConfig<TEvent extends EventObject> = {
>TransitionsConfig : TransitionsConfig<TEvent>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^

[K in TEvent["type"]]?: {
actions?: ActionFunction<Extract<TEvent, { type: K }>, TEvent>;
>actions : ActionFunction<Extract<TEvent, { type: K; }>, TEvent> | undefined
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^
>type : K
> : ^

};
};

declare function createMachine<TEvent extends EventObject>(config: {
>createMachine : <TEvent extends EventObject>(config: { types?: { events?: TEvent; }; on?: TransitionsConfig<NoInfer<TEvent>>; }) => void
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^
>config : { types?: { events?: TEvent; }; on?: TransitionsConfig<NoInfer<TEvent>>; }
> : ^^^^^^^^^^ ^^^^^^^ ^^^

types?: {
>types : { events?: TEvent; } | undefined
> : ^^^^^^^^^^^ ^^^^^^^^^^^^^^^

events?: TEvent;
>events : TEvent | undefined
> : ^^^^^^^^^^^^^^^^^^

};
on?: TransitionsConfig<NoInfer<TEvent>>;
>on : TransitionsConfig<NoInfer<TEvent>> | undefined
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

}): void;

createMachine({
>createMachine({ types: { events: {} as Input, }, on: { FOO: { actions: ({ event }) => { event; // { type: "FOO"; } }, }, },}) : void
> : ^^^^
>createMachine : <TEvent extends EventObject>(config: { types?: { events?: TEvent; }; on?: TransitionsConfig<NoInfer<TEvent>>; }) => void
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^
>{ types: { events: {} as Input, }, on: { FOO: { actions: ({ event }) => { event; // { type: "FOO"; } }, }, },} : { types: { events: Input; }; on: { FOO: { actions: ({ event }: { event: FooEvent; }) => void; }; }; }
> : ^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

types: {
>types : { events: Input; }
> : ^^^^^^^^^^ ^^^
>{ events: {} as Input, } : { events: Input; }
> : ^^^^^^^^^^ ^^^

events: {} as Input,
>events : Input
> : ^^^^^
>{} as Input : Input
> : ^^^^^
>{} : {}
> : ^^

},
on: {
>on : { FOO: { actions: ({ event }: { event: FooEvent; }) => void; }; }
> : ^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{ FOO: { actions: ({ event }) => { event; // { type: "FOO"; } }, }, } : { FOO: { actions: ({ event }: { event: FooEvent; }) => void; }; }
> : ^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

FOO: {
>FOO : { actions: ({ event }: { event: FooEvent; }) => void; }
> : ^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{ actions: ({ event }) => { event; // { type: "FOO"; } }, } : { actions: ({ event }: { event: FooEvent; }) => void; }
> : ^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

actions: ({ event }) => {
>actions : ({ event }: { event: FooEvent; }) => void
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>({ event }) => { event; // { type: "FOO"; } } : ({ event }: { event: FooEvent; }) => void
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>event : FooEvent
> : ^^^^^^^^

event; // { type: "FOO"; }
>event : FooEvent
> : ^^^^^^^^

},
},
},
});

Loading