Skip to content

Commit 2a34a2c

Browse files
committed
Fixed an issue with contextual type for intersection properties
1 parent 9579ab8 commit 2a34a2c

4 files changed

+22
-45
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26811,16 +26811,25 @@ namespace ts {
2681126811
}
2681226812

2681326813
function getTypeOfPropertyOfContextualType(type: Type, name: __String, nameType?: Type) {
26814-
return mapType(type, t => {
26814+
return mapType(type, function propertyOfContextualTypeMapper(t): Type | undefined {
2681526815
if (isGenericMappedType(t) && !t.declaration.nameType) {
2681626816
const constraint = getConstraintTypeFromMappedType(t);
2681726817
const constraintOfConstraint = getBaseConstraintOfType(constraint) || constraint;
2681826818
const propertyNameType = nameType || getStringLiteralType(unescapeLeadingUnderscores(name));
2681926819
if (isTypeAssignableTo(propertyNameType, constraintOfConstraint)) {
2682026820
return substituteIndexedMappedType(t, propertyNameType);
2682126821
}
26822+
return undefined;
26823+
}
26824+
if (t.flags & TypeFlags.Intersection) {
26825+
const intersection = t as IntersectionType;
26826+
const newTypes = intersection.types.map(propertyOfContextualTypeMapper).filter((t): t is Type => !!t);
26827+
if (newTypes.length === 0) {
26828+
return undefined;
26829+
}
26830+
return getIntersectionType(newTypes);
2682226831
}
26823-
else if (t.flags & TypeFlags.StructuredType) {
26832+
if (t.flags & TypeFlags.StructuredType) {
2682426833
const prop = getPropertyOfType(t, name);
2682526834
if (prop) {
2682626835
return isCircularMappedProperty(prop) ? undefined : getTypeOfSymbol(prop);

tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.errors.txt

Lines changed: 0 additions & 34 deletions
This file was deleted.

tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.symbols

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ createMachine({
7272
>ev : Symbol(ev, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 22, 10))
7373

7474
ev.type; // should be 'FOO'
75+
>ev.type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 19, 19))
7576
>ev : Symbol(ev, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 22, 10))
77+
>type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 19, 19))
7678

7779
},
7880
},

tests/baselines/reference/contextualTypeFunctionObjectPropertyIntersection.types

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ declare function createMachine<TEvent extends { type: string }>(
3939
createMachine({
4040
>createMachine({ schema: { events: {} as { type: "FOO" } | { type: "BAR" }, }, on: { FOO: (ev) => { ev.type; // should be 'FOO' }, },}) : void
4141
>createMachine : <TEvent extends { type: string; }>(config: MachineConfig<TEvent>) => void
42-
>{ schema: { events: {} as { type: "FOO" } | { type: "BAR" }, }, on: { FOO: (ev) => { ev.type; // should be 'FOO' }, },} : { schema: { events: { type: "FOO"; } | { type: "BAR"; }; }; on: { FOO: (ev: any) => void; }; }
42+
>{ 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; }; }
4343

4444
schema: {
4545
>schema : { events: { type: "FOO"; } | { type: "BAR"; }; }
@@ -54,18 +54,18 @@ createMachine({
5454

5555
},
5656
on: {
57-
>on : { FOO: (ev: any) => void; }
58-
>{ FOO: (ev) => { ev.type; // should be 'FOO' }, } : { FOO: (ev: any) => void; }
57+
>on : { FOO: (ev: { type: "FOO"; }) => void; }
58+
>{ FOO: (ev) => { ev.type; // should be 'FOO' }, } : { FOO: (ev: { type: "FOO"; }) => void; }
5959

6060
FOO: (ev) => {
61-
>FOO : (ev: any) => void
62-
>(ev) => { ev.type; // should be 'FOO' } : (ev: any) => void
63-
>ev : any
61+
>FOO : (ev: { type: "FOO"; }) => void
62+
>(ev) => { ev.type; // should be 'FOO' } : (ev: { type: "FOO"; }) => void
63+
>ev : { type: "FOO"; }
6464

6565
ev.type; // should be 'FOO'
66-
>ev.type : any
67-
>ev : any
68-
>type : any
66+
>ev.type : "FOO"
67+
>ev : { type: "FOO"; }
68+
>type : "FOO"
6969

7070
},
7171
},

0 commit comments

Comments
 (0)