forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed an issue with contextual type for intersection properties (micr…
…osoft#48668) * Add a failing test case for contextual type not provided for functions comming from a property with intersection type * Fixed an issue with contextual type for intersection properties * Fixed how type of a property of a contextual type is being computed when intersections with indexers are used
- Loading branch information
Showing
5 changed files
with
256 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//// [contextualTypeFunctionObjectPropertyIntersection.ts] | ||
type Action<TEvent extends { type: string }> = (ev: TEvent) => void; | ||
|
||
interface MachineConfig<TEvent extends { type: string }> { | ||
schema: { | ||
events: TEvent; | ||
}; | ||
on?: { | ||
[K in TEvent["type"]]?: Action<TEvent extends { type: K } ? TEvent : never>; | ||
} & { | ||
"*"?: Action<TEvent>; | ||
}; | ||
} | ||
|
||
declare function createMachine<TEvent extends { type: string }>( | ||
config: MachineConfig<TEvent> | ||
): void; | ||
|
||
createMachine({ | ||
schema: { | ||
events: {} as { type: "FOO" } | { type: "BAR" }, | ||
}, | ||
on: { | ||
FOO: (ev) => { | ||
ev.type; // should be 'FOO' | ||
}, | ||
}, | ||
}); | ||
|
||
|
||
//// [contextualTypeFunctionObjectPropertyIntersection.js] | ||
"use strict"; | ||
createMachine({ | ||
schema: { | ||
events: {} | ||
}, | ||
on: { | ||
FOO: function (ev) { | ||
ev.type; // should be 'FOO' | ||
} | ||
} | ||
}); |
82 changes: 82 additions & 0 deletions
82
tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
=== tests/cases/compiler/contextualTypeFunctionObjectPropertyIntersection.ts === | ||
type Action<TEvent extends { type: string }> = (ev: TEvent) => void; | ||
>Action : Symbol(Action, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 0)) | ||
>TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 12)) | ||
>type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 28)) | ||
>ev : Symbol(ev, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 48)) | ||
>TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 12)) | ||
|
||
interface MachineConfig<TEvent extends { type: string }> { | ||
>MachineConfig : Symbol(MachineConfig, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 68)) | ||
>TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 24)) | ||
>type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 40)) | ||
|
||
schema: { | ||
>schema : Symbol(MachineConfig.schema, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 58)) | ||
|
||
events: TEvent; | ||
>events : Symbol(events, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 3, 11)) | ||
>TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 24)) | ||
|
||
}; | ||
on?: { | ||
>on : Symbol(MachineConfig.on, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 5, 4)) | ||
|
||
[K in TEvent["type"]]?: Action<TEvent extends { type: K } ? TEvent : never>; | ||
>K : Symbol(K, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 7, 5)) | ||
>TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 24)) | ||
>Action : Symbol(Action, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 0)) | ||
>TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 24)) | ||
>type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 7, 51)) | ||
>K : Symbol(K, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 7, 5)) | ||
>TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 24)) | ||
|
||
} & { | ||
"*"?: Action<TEvent>; | ||
>"*" : Symbol("*", Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 8, 7)) | ||
>Action : Symbol(Action, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 0)) | ||
>TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 24)) | ||
|
||
}; | ||
} | ||
|
||
declare function createMachine<TEvent extends { type: string }>( | ||
>createMachine : Symbol(createMachine, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 11, 1)) | ||
>TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 13, 31)) | ||
>type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 13, 47)) | ||
|
||
config: MachineConfig<TEvent> | ||
>config : Symbol(config, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 13, 64)) | ||
>MachineConfig : Symbol(MachineConfig, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 68)) | ||
>TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 13, 31)) | ||
|
||
): void; | ||
|
||
createMachine({ | ||
>createMachine : Symbol(createMachine, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 11, 1)) | ||
|
||
schema: { | ||
>schema : Symbol(schema, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 17, 15)) | ||
|
||
events: {} as { type: "FOO" } | { type: "BAR" }, | ||
>events : Symbol(events, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 18, 11)) | ||
>type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 19, 19)) | ||
>type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 19, 37)) | ||
|
||
}, | ||
on: { | ||
>on : Symbol(on, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 20, 4)) | ||
|
||
FOO: (ev) => { | ||
>FOO : Symbol(FOO, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 21, 7)) | ||
>ev : Symbol(ev, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 22, 10)) | ||
|
||
ev.type; // should be 'FOO' | ||
>ev.type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 19, 19)) | ||
>ev : Symbol(ev, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 22, 10)) | ||
>type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 19, 19)) | ||
|
||
}, | ||
}, | ||
}); | ||
|
73 changes: 73 additions & 0 deletions
73
tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
=== tests/cases/compiler/contextualTypeFunctionObjectPropertyIntersection.ts === | ||
type Action<TEvent extends { type: string }> = (ev: TEvent) => void; | ||
>Action : Action<TEvent> | ||
>type : string | ||
>ev : TEvent | ||
|
||
interface MachineConfig<TEvent extends { type: string }> { | ||
>type : string | ||
|
||
schema: { | ||
>schema : { events: TEvent; } | ||
|
||
events: TEvent; | ||
>events : TEvent | ||
|
||
}; | ||
on?: { | ||
>on : ({ [K in TEvent["type"]]?: Action<TEvent extends { type: K; } ? TEvent : never> | undefined; } & { "*"?: Action<TEvent> | undefined; }) | undefined | ||
|
||
[K in TEvent["type"]]?: Action<TEvent extends { type: K } ? TEvent : never>; | ||
>type : K | ||
|
||
} & { | ||
"*"?: Action<TEvent>; | ||
>"*" : Action<TEvent> | undefined | ||
|
||
}; | ||
} | ||
|
||
declare function createMachine<TEvent extends { type: string }>( | ||
>createMachine : <TEvent extends { type: string; }>(config: MachineConfig<TEvent>) => void | ||
>type : string | ||
|
||
config: MachineConfig<TEvent> | ||
>config : MachineConfig<TEvent> | ||
|
||
): void; | ||
|
||
createMachine({ | ||
>createMachine({ schema: { events: {} as { type: "FOO" } | { type: "BAR" }, }, on: { FOO: (ev) => { ev.type; // should be 'FOO' }, },}) : void | ||
>createMachine : <TEvent extends { type: string; }>(config: MachineConfig<TEvent>) => void | ||
>{ schema: { events: {} as { type: "FOO" } | { type: "BAR" }, }, on: { FOO: (ev) => { ev.type; // should be 'FOO' }, },} : { schema: { events: { type: "FOO"; } | { type: "BAR"; }; }; on: { FOO: (ev: { type: "FOO"; }) => void; }; } | ||
|
||
schema: { | ||
>schema : { events: { type: "FOO"; } | { type: "BAR"; }; } | ||
>{ events: {} as { type: "FOO" } | { type: "BAR" }, } : { events: { type: "FOO"; } | { type: "BAR"; }; } | ||
|
||
events: {} as { type: "FOO" } | { type: "BAR" }, | ||
>events : { type: "FOO"; } | { type: "BAR"; } | ||
>{} as { type: "FOO" } | { type: "BAR" } : { type: "FOO"; } | { type: "BAR"; } | ||
>{} : {} | ||
>type : "FOO" | ||
>type : "BAR" | ||
|
||
}, | ||
on: { | ||
>on : { FOO: (ev: { type: "FOO"; }) => void; } | ||
>{ FOO: (ev) => { ev.type; // should be 'FOO' }, } : { FOO: (ev: { type: "FOO"; }) => void; } | ||
|
||
FOO: (ev) => { | ||
>FOO : (ev: { type: "FOO"; }) => void | ||
>(ev) => { ev.type; // should be 'FOO' } : (ev: { type: "FOO"; }) => void | ||
>ev : { type: "FOO"; } | ||
|
||
ev.type; // should be 'FOO' | ||
>ev.type : "FOO" | ||
>ev : { type: "FOO"; } | ||
>type : "FOO" | ||
|
||
}, | ||
}, | ||
}); | ||
|
29 changes: 29 additions & 0 deletions
29
tests/cases/compiler/contextualTypeFunctionObjectPropertyIntersection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// @strict: true | ||
|
||
type Action<TEvent extends { type: string }> = (ev: TEvent) => void; | ||
|
||
interface MachineConfig<TEvent extends { type: string }> { | ||
schema: { | ||
events: TEvent; | ||
}; | ||
on?: { | ||
[K in TEvent["type"]]?: Action<TEvent extends { type: K } ? TEvent : never>; | ||
} & { | ||
"*"?: Action<TEvent>; | ||
}; | ||
} | ||
|
||
declare function createMachine<TEvent extends { type: string }>( | ||
config: MachineConfig<TEvent> | ||
): void; | ||
|
||
createMachine({ | ||
schema: { | ||
events: {} as { type: "FOO" } | { type: "BAR" }, | ||
}, | ||
on: { | ||
FOO: (ev) => { | ||
ev.type; // should be 'FOO' | ||
}, | ||
}, | ||
}); |