Skip to content

TypeScript allows invalid intersection type with index signatureΒ #47456

@srmagura

Description

@srmagura

Bug Report

πŸ”Ž Search Terms

intersection type, index signature

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ

Tested in TS Playground for version 3.9 up to 4.6 nightly.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Grammar = {
    rest: 'rest' // Error: "rest" is not assignable to "value"
    [key: string]: 'value'
}

// No error - shouldn't this behave the same way?
type Grammar2 = {
    rest: 'rest'
} & {
    [key: string]: 'value'
}

// Compile error - Grammar2 type is essentially unusable
const grammar2: Grammar2 = {
    rest: 'rest',
    myKey: 'value'
}

πŸ™ Actual behavior

  • Only Grammar produces a compile error even though it is equivalent to Grammar2.
  • 'rest' cannot be assigned to the rest property of Grammar2.

πŸ™‚ Expected behavior

I'm not sure how this should behave.

Additional Context

Posting this issue due to a problem with @types/prismjs. A Prism Grammar has a [key: string]: GrammarValue index signature AND special properties like rest which are not assignable to GrammarValue. How can I fix this in @types/prismjs?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions