Skip to content

Ignore any in contextual intersection types #59528

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
wants to merge 2 commits into from
Closed
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
30 changes: 20 additions & 10 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31578,30 +31578,40 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function getTypeOfPropertyOfContextualType(type: Type, name: __String, nameType?: Type) {
return mapType(type, t => {
if (isGenericMappedType(t) && !t.declaration.nameType) {
const constraint = getConstraintTypeFromMappedType(t);
return mapType(type, getTypeOfProperty, /*noReductions*/ true);
function getTypeOfProperty(type: Type): Type | undefined {
if (type.flags & TypeFlags.Intersection) {
// Intersections that include `any` resolve to `any`. This loses potentially useful type
// information contributed by other types in the intersection, so we map `any` to `unknown`
// when we intersect the contextual property types.
return getIntersectionType(map((type as IntersectionType).types, t => {
const propType = getTypeOfProperty(t);
return !propType || propType.flags & TypeFlags.Any ? unknownType : propType;
}));
}
else if (isGenericMappedType(type) && !type.declaration.nameType) {
const constraint = getConstraintTypeFromMappedType(type);
const constraintOfConstraint = getBaseConstraintOfType(constraint) || constraint;
const propertyNameType = nameType || getStringLiteralType(unescapeLeadingUnderscores(name));
if (isTypeAssignableTo(propertyNameType, constraintOfConstraint)) {
return substituteIndexedMappedType(t, propertyNameType);
return substituteIndexedMappedType(type, propertyNameType);
}
}
else if (t.flags & TypeFlags.StructuredType) {
const prop = getPropertyOfType(t, name);
else if (type.flags & TypeFlags.Object) {
const prop = getPropertyOfType(type, name);
if (prop) {
return isCircularMappedProperty(prop) ? undefined : removeMissingType(getTypeOfSymbol(prop), !!(prop.flags & SymbolFlags.Optional));
}
if (isTupleType(t) && isNumericLiteralName(name) && +name >= 0) {
const restType = getElementTypeOfSliceOfTupleType(t, t.target.fixedLength, /*endSkipCount*/ 0, /*writing*/ false, /*noReductions*/ true);
if (isTupleType(type) && isNumericLiteralName(name) && +name >= 0) {
const restType = getElementTypeOfSliceOfTupleType(type, type.target.fixedLength, /*endSkipCount*/ 0, /*writing*/ false, /*noReductions*/ true);
if (restType) {
return restType;
}
}
return findApplicableIndexInfo(getIndexInfosOfStructuredType(t), nameType || getStringLiteralType(unescapeLeadingUnderscores(name)))?.type;
return findApplicableIndexInfo(getIndexInfosOfStructuredType(type), nameType || getStringLiteralType(unescapeLeadingUnderscores(name)))?.type;
}
return undefined;
}, /*noReductions*/ true);
}
}

// In an object literal contextually typed by a type T, the contextual type of a property assignment is the type of
Expand Down
129 changes: 129 additions & 0 deletions tests/baselines/reference/contextualIntersectionTypes.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
//// [tests/cases/compiler/contextualIntersectionTypes.ts] ////

=== contextualIntersectionTypes.ts ===
// https://github.com/microsoft/TypeScript/issues/59473

const a: [any] & [1] = [1];
>a : Symbol(a, Decl(contextualIntersectionTypes.ts, 2, 5))

const b: { ml: any } & { ml: 'edge' } = { ml: 'edge' };
>b : Symbol(b, Decl(contextualIntersectionTypes.ts, 3, 5))
>ml : Symbol(ml, Decl(contextualIntersectionTypes.ts, 3, 10))
>ml : Symbol(ml, Decl(contextualIntersectionTypes.ts, 3, 24))
>ml : Symbol(ml, Decl(contextualIntersectionTypes.ts, 3, 41))

// https://github.com/microsoft/TypeScript/issues/48812

type Action<TEvent extends { type: string }> = (ev: TEvent) => void;
>Action : Symbol(Action, Decl(contextualIntersectionTypes.ts, 3, 55))
>TEvent : Symbol(TEvent, Decl(contextualIntersectionTypes.ts, 7, 12))
>type : Symbol(type, Decl(contextualIntersectionTypes.ts, 7, 28))
>ev : Symbol(ev, Decl(contextualIntersectionTypes.ts, 7, 48))
>TEvent : Symbol(TEvent, Decl(contextualIntersectionTypes.ts, 7, 12))

interface MachineConfig<TEvent extends { type: string }> {
>MachineConfig : Symbol(MachineConfig, Decl(contextualIntersectionTypes.ts, 7, 68))
>TEvent : Symbol(TEvent, Decl(contextualIntersectionTypes.ts, 9, 24))
>type : Symbol(type, Decl(contextualIntersectionTypes.ts, 9, 40))

schema: {
>schema : Symbol(MachineConfig.schema, Decl(contextualIntersectionTypes.ts, 9, 58))

events: TEvent;
>events : Symbol(events, Decl(contextualIntersectionTypes.ts, 10, 11))
>TEvent : Symbol(TEvent, Decl(contextualIntersectionTypes.ts, 9, 24))

};
on?: {
>on : Symbol(MachineConfig.on, Decl(contextualIntersectionTypes.ts, 12, 4))

[K in TEvent["type"]]?: Action<TEvent extends { type: K } ? TEvent : never>;
>K : Symbol(K, Decl(contextualIntersectionTypes.ts, 14, 5))
>TEvent : Symbol(TEvent, Decl(contextualIntersectionTypes.ts, 9, 24))
>Action : Symbol(Action, Decl(contextualIntersectionTypes.ts, 3, 55))
>TEvent : Symbol(TEvent, Decl(contextualIntersectionTypes.ts, 9, 24))
>type : Symbol(type, Decl(contextualIntersectionTypes.ts, 14, 51))
>K : Symbol(K, Decl(contextualIntersectionTypes.ts, 14, 5))
>TEvent : Symbol(TEvent, Decl(contextualIntersectionTypes.ts, 9, 24))

} & {
"*"?: Action<TEvent>;
>"*" : Symbol("*", Decl(contextualIntersectionTypes.ts, 15, 7))
>Action : Symbol(Action, Decl(contextualIntersectionTypes.ts, 3, 55))
>TEvent : Symbol(TEvent, Decl(contextualIntersectionTypes.ts, 9, 24))

};
}

declare function createMachine<TEvent extends { type: string }>(
>createMachine : Symbol(createMachine, Decl(contextualIntersectionTypes.ts, 18, 1))
>TEvent : Symbol(TEvent, Decl(contextualIntersectionTypes.ts, 20, 31))
>type : Symbol(type, Decl(contextualIntersectionTypes.ts, 20, 47))

config: MachineConfig<TEvent>
>config : Symbol(config, Decl(contextualIntersectionTypes.ts, 20, 64))
>MachineConfig : Symbol(MachineConfig, Decl(contextualIntersectionTypes.ts, 7, 68))
>TEvent : Symbol(TEvent, Decl(contextualIntersectionTypes.ts, 20, 31))

): void;

createMachine({
>createMachine : Symbol(createMachine, Decl(contextualIntersectionTypes.ts, 18, 1))

schema: {
>schema : Symbol(schema, Decl(contextualIntersectionTypes.ts, 24, 15))

events: {} as { type: "FOO" } | { type: "BAR" },
>events : Symbol(events, Decl(contextualIntersectionTypes.ts, 25, 11))
>type : Symbol(type, Decl(contextualIntersectionTypes.ts, 26, 19))
>type : Symbol(type, Decl(contextualIntersectionTypes.ts, 26, 37))

},
on: {
>on : Symbol(on, Decl(contextualIntersectionTypes.ts, 27, 4))

FOO: (ev) => {
>FOO : Symbol(FOO, Decl(contextualIntersectionTypes.ts, 28, 7))
>ev : Symbol(ev, Decl(contextualIntersectionTypes.ts, 29, 10))

ev.type;
>ev.type : Symbol(type, Decl(contextualIntersectionTypes.ts, 26, 19))
>ev : Symbol(ev, Decl(contextualIntersectionTypes.ts, 29, 10))
>type : Symbol(type, Decl(contextualIntersectionTypes.ts, 26, 19))

},
},
});

// https://github.com/microsoft/TypeScript/issues/49307#issuecomment-1196014488

type Validate<T> = T & { [K in keyof T]: object }
>Validate : Symbol(Validate, Decl(contextualIntersectionTypes.ts, 33, 3))
>T : Symbol(T, Decl(contextualIntersectionTypes.ts, 37, 14))
>T : Symbol(T, Decl(contextualIntersectionTypes.ts, 37, 14))
>K : Symbol(K, Decl(contextualIntersectionTypes.ts, 37, 26))
>T : Symbol(T, Decl(contextualIntersectionTypes.ts, 37, 14))

declare function f<S, T extends Record<string, (state: S) => any>>(s: S, x: Validate<T>): void;
>f : Symbol(f, Decl(contextualIntersectionTypes.ts, 37, 49))
>S : Symbol(S, Decl(contextualIntersectionTypes.ts, 38, 19))
>T : Symbol(T, Decl(contextualIntersectionTypes.ts, 38, 21))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
>state : Symbol(state, Decl(contextualIntersectionTypes.ts, 38, 48))
>S : Symbol(S, Decl(contextualIntersectionTypes.ts, 38, 19))
>s : Symbol(s, Decl(contextualIntersectionTypes.ts, 38, 67))
>S : Symbol(S, Decl(contextualIntersectionTypes.ts, 38, 19))
>x : Symbol(x, Decl(contextualIntersectionTypes.ts, 38, 72))
>Validate : Symbol(Validate, Decl(contextualIntersectionTypes.ts, 33, 3))
>T : Symbol(T, Decl(contextualIntersectionTypes.ts, 38, 21))

f(0, {
>f : Symbol(f, Decl(contextualIntersectionTypes.ts, 37, 49))

foo: s => s + 1,
>foo : Symbol(foo, Decl(contextualIntersectionTypes.ts, 40, 6))
>s : Symbol(s, Decl(contextualIntersectionTypes.ts, 41, 6))
>s : Symbol(s, Decl(contextualIntersectionTypes.ts, 41, 6))

})

172 changes: 172 additions & 0 deletions tests/baselines/reference/contextualIntersectionTypes.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
//// [tests/cases/compiler/contextualIntersectionTypes.ts] ////

=== contextualIntersectionTypes.ts ===
// https://github.com/microsoft/TypeScript/issues/59473

const a: [any] & [1] = [1];
>a : [any] & [1]
> : ^^^^^^^^^^^
>[1] : [1]
> : ^^^
>1 : 1
> : ^

const b: { ml: any } & { ml: 'edge' } = { ml: 'edge' };
>b : { ml: any; } & { ml: "edge"; }
> : ^^^^^^ ^^^^^^^^^^^^ ^^^
>ml : any
>ml : "edge"
> : ^^^^^^
>{ ml: 'edge' } : { ml: "edge"; }
> : ^^^^^^^^^^^^^^^
>ml : "edge"
> : ^^^^^^
>'edge' : "edge"
> : ^^^^^^

// https://github.com/microsoft/TypeScript/issues/48812

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
> : ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^

[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; }, },}) : void
> : ^^^^
>createMachine : <TEvent extends { type: string; }>(config: MachineConfig<TEvent>) => void
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^
>{ schema: { events: {} as { type: "FOO" } | { type: "BAR" }, }, on: { FOO: (ev) => { ev.type; }, },} : { 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; }, } : { FOO: (ev: { type: "FOO"; }) => void; }
> : ^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^^^^^

FOO: (ev) => {
>FOO : (ev: { type: "FOO"; }) => void
> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^
>(ev) => { ev.type; } : (ev: { type: "FOO"; }) => void
> : ^ ^^^^^^^^^^ ^^^^^^^^^^^^
>ev : { type: "FOO"; }
> : ^^^^^^^^ ^^^

ev.type;
>ev.type : "FOO"
> : ^^^^^
>ev : { type: "FOO"; }
> : ^^^^^^^^ ^^^
>type : "FOO"
> : ^^^^^

},
},
});

// https://github.com/microsoft/TypeScript/issues/49307#issuecomment-1196014488

type Validate<T> = T & { [K in keyof T]: object }
>Validate : Validate<T>
> : ^^^^^^^^^^^

declare function f<S, T extends Record<string, (state: S) => any>>(s: S, x: Validate<T>): void;
>f : <S, T extends Record<string, (state: S) => any>>(s: S, x: Validate<T>) => void
> : ^ ^^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^
>state : S
> : ^
>s : S
> : ^
>x : Validate<T>
> : ^^^^^^^^^^^

f(0, {
>f(0, { foo: s => s + 1,}) : void
> : ^^^^
>f : <S, T extends Record<string, (state: S) => any>>(s: S, x: Validate<T>) => void
> : ^ ^^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^
>0 : 0
> : ^
>{ foo: s => s + 1,} : { foo: (s: number) => number; }
> : ^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^

foo: s => s + 1,
>foo : (s: number) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>s => s + 1 : (s: number) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>s : number
> : ^^^^^^
>s + 1 : number
> : ^^^^^^
>s : number
> : ^^^^^^
>1 : 1
> : ^

})

Loading