2021年2月7日星期日

Typescript: Is there a way to use variables in a template literal type value?

I'd like to pass variables into a template literal type value, but I can't seem to get it to work (you can play with the following example here). Is this possible?

type Bar = "a" | "b"    interface Foo {label: `${Bar} (${number})`}    const x: Foo = {label: "a (3)"}; // fine    const myBar: Bar = "b";  const myNum: number = 3;  const y: Foo = {label: `${myBar} (${myNum})`}; // errors  

Is there any way of getting y to compile?

https://stackoverflow.com/questions/66093406/typescript-is-there-a-way-to-use-variables-in-a-template-literal-type-value February 08, 2021 at 05:42AM

没有评论:

发表评论