microsoft / microsoft/TypeScript
Incomplete infer for generic types
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 4.0.5
Search Terms:
typescript, infer, incomplete, unknown, generic
Expected behavior:
The compiler should be able to extract information of the generic Item type from the function definition and infer string.
Actual behavior:
The compiler is able to infer the Names type as { id: string } but the Item type is unknown.
Related Issues:
Code
interface LoaderConfig<Key, Item> {
loadItems: (keys: readonly Key[]) => Promise<readonly Item[]>;
getKey: (item: Item) => Key;
makeCacheKey: (key: Key) => string;
}
type LoaderConfigByName<Names, Item> = {
[name in keyof Names]: LoaderConfig<Names[name], Item>;
};
function makeLoader<Names, Item>(
configs: LoaderConfigByName<Names, Item>
): void {
console.log(configs);
}
makeLoader({
id: {
loadItems: async (keys: readonly string[]): Promise<readonly string[]> => {
return await Promise.resolve(keys);
},
getKey: (item: string): string => item,
makeCacheKey: (key: string): string => {
return key;
}
}
});
Output
"use strict";
function makeLoader(configs) {
console.log(configs);
}
makeLoader({
id: {
loadItems: async (keys) => {
return await Promise.resolve(keys);
},
getKey: (item) => item,
makeCacheKey: (key) => {
return key;
}
}
});
Compiler Options
{
"compilerOptions": {
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"moduleResolution": 2,
"target": "ES2017",
"jsx": "React",
"module": "ESNext"
}
}
Playground Link: Provided
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided Playground link and the generic makeLoader example using TypeScript 4.0.5. Reproduce the inference result and trace the compiler's handling of the generic Item type. Done means the example infers Item as string while preserving the inferred Names type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100