microsoft / microsoft/TypeScript
Interface merging for any property names does not allow type narrowing
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.1.0-dev.20201002
Search Terms: interface merging merge object any name subsequent property declarations
Code
interface PropsContainer {
props: {
[propName: string]: any;
}
}
interface PropsContainer {
props: {
[propName: string]: any;
specialProp: string;
}
}
Expected behavior:
This should be allowed. The merged interface should result in a type with props that have any prop name as any except for specialProp which is a string.
This would be useful in a case where a third party library (such as @types/react-test-renderer) declare an object type with { [propName: string]: any } syntax, but you want to narrow the value of the property for specific types via interface merging.
Note that even { specialProp?: any, [propName: string]: any } is not allowed even though the types are functionally identical in this case.
Actual behavior:
This results in a Subsequent property declarations must have the same type.
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 linked TypeScript Playground reproduction and confirm the diagnostic for the two merged PropsContainer declarations. Then trace the type-checking path for subsequent property declarations and index signatures. Done means the shown merge is accepted, specialProp narrows to string, and existing incompatible redeclarations remain rejected.
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