Skip to content
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

Fixed an issue with contextual type for intersection properties (take 2) #52095

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 54 additions & 18 deletions src/compiler/checker.ts
Expand Up @@ -30616,29 +30616,65 @@ 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);
const constraintOfConstraint = getBaseConstraintOfType(constraint) || constraint;
const propertyNameType = nameType || getStringLiteralType(unescapeLeadingUnderscores(name));
if (isTypeAssignableTo(propertyNameType, constraintOfConstraint)) {
return substituteIndexedMappedType(t, propertyNameType);
}
if (!(t.flags & TypeFlags.StructuredType)) {
return undefined;
}
else if (t.flags & TypeFlags.StructuredType) {
const prop = getPropertyOfType(t, 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 (restType) {
return restType;
if (t.flags & TypeFlags.Intersection) {
const intersection = t as IntersectionType;
let applicableIndexedMappedTypeSubstitions: Type[] | undefined;
let concretePropertyTypes: Type[] | undefined;
let applicableIndexInfoCandidates: Type[] | undefined;
for (const t of intersection.types) {
if (isGenericMappedType(t) && !t.declaration.nameType) {
applicableIndexedMappedTypeSubstitions = append(applicableIndexedMappedTypeSubstitions, getTypeOfApplicableIndexedMappedTypeSubstitutionOfContextualType(t));
continue;
}
const typeOfConcreteProperty = getTypeOfConcretePropertyOfContextualType(t);
if (typeOfConcreteProperty) {
concretePropertyTypes = append(concretePropertyTypes, typeOfConcreteProperty);
continue;
}
applicableIndexInfoCandidates = append(applicableIndexInfoCandidates, t);
}
if (concretePropertyTypes) {
return getIntersectionType(concatenate(concretePropertyTypes, applicableIndexedMappedTypeSubstitions));
}
const types = concatenate(mapDefined(applicableIndexInfoCandidates, getTypeOfApplicableIndexInfoOfContextualType), applicableIndexedMappedTypeSubstitions);
if (types.length > 0) {
return getIntersectionType(types);
}
return findApplicableIndexInfo(getIndexInfosOfStructuredType(t), nameType || getStringLiteralType(unescapeLeadingUnderscores(name)))?.type;
return undefined;
}
return undefined;
return isGenericMappedType(t) && !t.declaration.nameType
? getTypeOfApplicableIndexedMappedTypeSubstitutionOfContextualType(t)
: getTypeOfConcretePropertyOfContextualType(t) || getTypeOfApplicableIndexInfoOfContextualType(t);
}, /*noReductions*/ true);

function getTypeOfApplicableIndexedMappedTypeSubstitutionOfContextualType(t: MappedType) {
const constraint = getConstraintTypeFromMappedType(t);
const constraintOfConstraint = getBaseConstraintOfType(constraint) || constraint;
const propertyNameType = nameType || getStringLiteralType(unescapeLeadingUnderscores(name));
if (isTypeAssignableTo(propertyNameType, constraintOfConstraint)) {
return substituteIndexedMappedType(t, propertyNameType);
}
return undefined;
}
function getTypeOfConcretePropertyOfContextualType(t: Type) {
const prop = getPropertyOfType(t, 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 (restType) {
return restType;
}
}
return undefined;
}
function getTypeOfApplicableIndexInfoOfContextualType(t: Type) {
return findApplicableIndexInfo(getIndexInfosOfStructuredType(t), nameType || getStringLiteralType(unescapeLeadingUnderscores(name)))?.type;
}
}

// In an object literal contextually typed by a type T, the contextual type of a property assignment is the type of
Expand Down
@@ -0,0 +1,242 @@
//// [tests/cases/compiler/contextualTypeFunctionObjectPropertyIntersection.ts] ////

=== contextualTypeFunctionObjectPropertyIntersection.ts ===
// repro from #48812

type Action<TEvent extends { type: string }> = (ev: TEvent) => void;
>Action : Symbol(Action, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 0))
>TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 12))
>type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 28))
>ev : Symbol(ev, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 48))
>TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 12))

interface MachineConfig<TEvent extends { type: string }> {
>MachineConfig : Symbol(MachineConfig, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 68))
>TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 4, 24))
>type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 4, 40))

schema: {
>schema : Symbol(MachineConfig.schema, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 4, 58))

events: TEvent;
>events : Symbol(events, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 5, 11))
>TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 4, 24))

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

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

} & {
"*"?: Action<TEvent>;
>"*" : Symbol("*", Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 10, 7))
>Action : Symbol(Action, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 0, 0))
>TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 4, 24))

};
}

declare function createMachine<TEvent extends { type: string }>(
>createMachine : Symbol(createMachine, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 13, 1))
>TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 15, 31))
>type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 15, 47))

config: MachineConfig<TEvent>
>config : Symbol(config, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 15, 64))
>MachineConfig : Symbol(MachineConfig, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 2, 68))
>TEvent : Symbol(TEvent, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 15, 31))

): void;

createMachine({
>createMachine : Symbol(createMachine, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 13, 1))

schema: {
>schema : Symbol(schema, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 19, 15))

events: {} as { type: "FOO" } | { type: "BAR" },
>events : Symbol(events, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 20, 11))
>type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 21, 19))
>type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 21, 37))

},
on: {
>on : Symbol(on, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 22, 4))

FOO: (ev) => {
>FOO : Symbol(FOO, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 23, 7))
>ev : Symbol(ev, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 24, 10))

ev.type; // should be 'FOO'
>ev.type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 21, 19))
>ev : Symbol(ev, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 24, 10))
>type : Symbol(type, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 21, 19))

},
},
});

// repro from #49307#issuecomment-1143103607

declare function createSlice<T>(
>createSlice : Symbol(createSlice, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 28, 3), Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 63, 2))
>T : Symbol(T, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 32, 29))

reducers: { [K: string]: (state: string) => void } & {
>reducers : Symbol(reducers, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 32, 32))
>K : Symbol(K, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 33, 15))
>state : Symbol(state, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 33, 28))

[K in keyof T]: object;
>K : Symbol(K, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 34, 5))
>T : Symbol(T, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 32, 29))
}
): void;

createSlice({
>createSlice : Symbol(createSlice, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 28, 3), Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 63, 2))

f(a) {},
>f : Symbol(f, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 38, 13))
>a : Symbol(a, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 39, 4))

});

// repro from #49307#issuecomment-1196014488

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

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

f(0, {
>f : Symbol(f, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 44, 49))

foo: s => s + 1,
>foo : Symbol(foo, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 47, 6))
>s : Symbol(s, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 48, 6))
>s : Symbol(s, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 48, 6))

})

// repro from 49307#issuecomment-1195858950

type SliceCaseReducers<State> = Record<string, (state: State) => State | void>;
>SliceCaseReducers : Symbol(SliceCaseReducers, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 49, 2))
>State : Symbol(State, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 53, 23))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
>state : Symbol(state, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 53, 48))
>State : Symbol(State, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 53, 23))
>State : Symbol(State, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 53, 23))

type ValidateSliceCaseReducers<S, ACR extends SliceCaseReducers<S>> = ACR & {
>ValidateSliceCaseReducers : Symbol(ValidateSliceCaseReducers, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 53, 79))
>S : Symbol(S, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 55, 31))
>ACR : Symbol(ACR, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 55, 33))
>SliceCaseReducers : Symbol(SliceCaseReducers, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 49, 2))
>S : Symbol(S, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 55, 31))
>ACR : Symbol(ACR, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 55, 33))

[T in keyof ACR]: ACR[T] extends {
>T : Symbol(T, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 56, 3))
>ACR : Symbol(ACR, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 55, 33))
>ACR : Symbol(ACR, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 55, 33))
>T : Symbol(T, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 56, 3))

reducer(s: S, action?: infer A): any;
>reducer : Symbol(reducer, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 56, 36))
>s : Symbol(s, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 57, 12))
>S : Symbol(S, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 55, 31))
>action : Symbol(action, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 57, 17))
>A : Symbol(A, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 57, 32))
}
? {
prepare(...a: never[]): Omit<A, "type">;
>prepare : Symbol(prepare, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 59, 7))
>a : Symbol(a, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 60, 16))
>Omit : Symbol(Omit, Decl(lib.es5.d.ts, --, --))
>A : Symbol(A, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 57, 32))
}
: {};
};

declare function createSlice<
>createSlice : Symbol(createSlice, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 28, 3), Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 63, 2))

State,
>State : Symbol(State, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 65, 29))

CaseReducers extends SliceCaseReducers<State>
>CaseReducers : Symbol(CaseReducers, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 66, 8))
>SliceCaseReducers : Symbol(SliceCaseReducers, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 49, 2))
>State : Symbol(State, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 65, 29))

>(options: {
>options : Symbol(options, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 68, 2))

initialState: State | (() => State);
>initialState : Symbol(initialState, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 68, 12))
>State : Symbol(State, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 65, 29))
>State : Symbol(State, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 65, 29))

reducers: ValidateSliceCaseReducers<State, CaseReducers>;
>reducers : Symbol(reducers, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 69, 38))
>ValidateSliceCaseReducers : Symbol(ValidateSliceCaseReducers, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 53, 79))
>State : Symbol(State, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 65, 29))
>CaseReducers : Symbol(CaseReducers, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 66, 8))

}): void;

export const clientSlice = createSlice({
>clientSlice : Symbol(clientSlice, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 73, 12))
>createSlice : Symbol(createSlice, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 28, 3), Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 63, 2))

initialState: {
>initialState : Symbol(initialState, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 73, 40))

username: "",
>username : Symbol(username, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 74, 17))

isLoggedIn: false,
>isLoggedIn : Symbol(isLoggedIn, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 75, 17))

userId: "",
>userId : Symbol(userId, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 76, 22))

avatar: "",
>avatar : Symbol(avatar, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 77, 15))

},
reducers: {
>reducers : Symbol(reducers, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 79, 4))

onClientUserChanged(state) {},
>onClientUserChanged : Symbol(onClientUserChanged, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 80, 13))
>state : Symbol(state, Decl(contextualTypeFunctionObjectPropertyIntersection.ts, 81, 24))

},
});