I am attempting to define a string literal type that has a variable parameter in it.
eg:
type myStringType = 'a' | 'b' | 'c' | `d[${parameter}]`
I'd like the string d[parameter]
to accept anything which resembles d[foo]
or d[bar]
Edited: added back ticks to template literal, though i'm not attached to this being a template literal and if there is another solution...
Starting from Typescript 4.1, you can use template literal types .
type parameter = "foo" | "bar"
type myStringType = 'a' | 'b' | 'c' | `d[${parameter}]`
// type myStringType = "a" | "b" | "c" | "d[foo]" | "d[bar]"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.