microsoft / microsoft/TypeScript

Interface merging for any property names does not allow type narrowing

Open
#40,904 2 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
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.

Playground Link: https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgApQPYAcDOBhDcOUaZAbwChlktNcAucq65AbVuwDk4BbCRnGCigA5gF1GcEAE8A3MwC+FJRVCRYiFOmz5CYYiFKVqHBkxbIcWCAmBwANtqwB+STPkX2dbnwFDREshScorKQA

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.