Skip to content

Template literal in string pattern index signature errorΒ #46309

Closed
@davidbielik

Description

@davidbielik

Bug Report

πŸ”Ž Search Terms

template literal string pattern

πŸ•— Version & Regression Information

  • Typescript 4.4.3
  • Typescript 4.4.5-beta

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface IDocument {
  name: string;
  [added_: `added_${string}`]: number[] | undefined;
}

const tech1 = {
    uuid: '70b26275-5096-4e4b-9d50-3c965c9e5073',
};

// why does this error?
const doc: IDocument = {
  name: "",
  [`added_${tech1.uuid}`]: [19700101],
};

// but this doesn't?
const doc2: IDocument = {
  name: "",
  [`added_abc`]: [12345]
}

interface IDocument2 {
  [added_: `added_${string}`]: number[] | undefined;
}

// and this doesn't?
const doc3: IDocument2 = {
    [`added_${tech1.uuid}`]: [19700101],
}

πŸ™ Actual behavior

Type '{ [x: string]: string | number[]; name: string; }' is not assignable to type 'IDocument'.
  'string' and '`added_${string}`' index signatures are incompatible.
    Type 'string | number[]' is not assignable to type 'number[] | undefined'.
      Type 'string' is not assignable to type 'number[] | undefined'.

πŸ™‚ Expected behavior

I don't understand why there is an error. If i remove the "name" key, the error goes away.

If i replace the templated string with a literal string added_abc the error goes away

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions