microsoft / microsoft/TypeScript
Broken inference between index typed object and Record
Open
Nobody has claimed this yet.
In Discussion
Suggestion
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 2.2.2
Code
interface Errors {
[field: string]: {
key: string
message: string
}
}
const errors: Errors = {}
function genericObjectFunction<K extends string, V>(obj: Record<K, V>): [K, V][] {
return []
}
/*
Argument of type 'Errors' is not assignable to parameter of type 'Record<string, never>'.
Index signatures are incompatible.
Type '{ key: string; message: string; }' is not assignable to type 'never'.
*/
genericObjectFunction(errors)
The types seem generally compatible though, as expected this is legal:
const errorRecord: Record<string, { key: string, message: string }> = errors
Expected behavior:
genericObjectFunction(errors) should compile.
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 minimal TypeScript 2.2.2 reproduction in the issue, focusing on genericObjectFunction and inference from the Errors index signature to Record. Done means genericObjectFunction(errors) compiles while preserving the demonstrated Record compatibility.
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
- 35/100