-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Inside a generic function with a remapped type as parameter, elements are wrongfully inferred as {}.Β #63363
Copy link
Copy link
Open
Labels
Needs InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.
Milestone
Description
π Search Terms
remapped
key mapping
generics
Maybe related:
- Incorrect substitution on index access with generic key type on generic mapped type with key remapping Β #47794
- Contextual type of a property type not inferred with key remappingΒ #48855
π Version & Regression Information
- tested on playground v6.0.2
β― Playground Link
π» Code
type RenameKeys<H extends Record<string, any>> = {
[K in string&keyof H as `on${Capitalize<K>}`]: () => void
};
function foo<T extends Record<string, any>>(handlers: RenameKeys<T>) {
for(const name in handlers) {
let handler: () => void = handlers[name]!; // nok
// inferred as {}Β instead of () => void;
// Type 'NonNullable<RenameKeys<T>[Extract<keyof RenameKeys<T>, string>]>' is not assignable to type '() => void'.
// Type '{}' provides no match for the signature '(): void'.(2322)
}
}π Actual behavior
The type of the elements of the remapped type is {} inside the generic function.
If we return the element, the returned type is correct (cf playground).
π Expected behavior
The type should be either:
() = void(correct)unknownorany: TS is unable to properly infer the type.
Having {} is quite strange/surprising.
Additional information about the issue
There is another issue with [K in string&keyof H] which doesn't manifest when we use [K in keyof H] (cf playground):
type RenameKeys<H extends Record<string, any[]>> = {
[K in string&keyof H]: (...args: H[K]) => void
};
function foo<T extends Record<string, any>>(handlers: RenameKeys<T>) {
for(const name in handlers)
return handlers[name]!;
throw new Error("ok");
}
const x = foo({onFoo: (e: 42) => {}}); // nok : (...args: unknown) => voidReactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Needs InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.