Skip to content

Template literal in string pattern index signature error #46309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
davidbielik opened this issue Oct 11, 2021 · 4 comments
Closed

Template literal in string pattern index signature error #46309

davidbielik opened this issue Oct 11, 2021 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@davidbielik
Copy link

davidbielik commented Oct 11, 2021

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

@jcalz
Copy link
Contributor

jcalz commented Oct 11, 2021

I think I've explained what's going on in my answer to your SO question.

As it stands the code here fails because `added_${tech1.uuid}` is seen as type string unless you use a const assertion (as per #42588). If you add the const assertion like `added_${tech1.uuid}` as const then it still doesn't work because of #13948, because the type `added_${string}` in a computed property gets widened to string.

To the extent that this issue differs from #13948, maybe it can be seen as a suggestion not to widen pattern template literal computed keys (as opposed to unions of string literals)?

@davidbielik
Copy link
Author

Hey @jcalz thanks for your help here. how would I workaround this if the uuid is not a constant, i.e. the output of a uuid generator?

See this for example

@jcalz
Copy link
Contributor

jcalz commented Oct 11, 2021

I think Stack Overflow is better suited to Q/A than GitHub is. The easiest workaround is just a type assertion, otherwise you can try to build helper functions to make up for the missing functionality, like this. My answer in Stack Overflow goes through these.

@DanielRosenwasser DanielRosenwasser added the Question An issue which isn't directly actionable in code label Oct 11, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow or the TypeScript Discord community.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants