Open
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Contextual TypesThe issue relates to contextual typesThe issue relates to contextual types
Milestone
Description
Bug Report
🔎 Search Terms
contextual, template literal, key remapping, object property
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
Playground link with relevant code
💻 Code
interface MachineConfig<TEvent extends { type: string }> {
schema?: {
events?: TEvent;
};
on?: {
[K in TEvent["type"] as `on${K}`]?: (
ev: TEvent extends { type: K } ? TEvent : never
) => void;
};
}
declare function createMachine<TEvent extends { type: string }>(
config: MachineConfig<TEvent>
): void;
export default createMachine({
schema: {
events: {} as { type: "FOO" },
},
on: {
onFOO: (ev) => {
ev.type; // should be `FOO`, but `ev` is an implicit `any`
},
},
});🙁 Actual behavior
Implicit any is reported
🙂 Expected behavior
ev should get inferred as { type: 'FOO' }
getTypeOfPropertyOfContextualType explicitly~ ignores this case:
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);
}
}Note that nameTypecomes from an argument provided by this call within getContextualTypeForObjectLiteralElement but it's undefined:
getTypeOfPropertyOfContextualType(type, symbol.escapedName, getSymbolLinks(symbol).nameType);I guess the fix here would be to not ignore this potential substitution based on !t.declaration.nameType and construct the possible K constraint based on the t.declaration.nameType and name. I don't know how to do this in a sufficiently generic manner though.
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Contextual TypesThe issue relates to contextual typesThe issue relates to contextual types
Activity
andrewbranch commentedon Apr 27, 2022
This reminds me of #45700