是否可以将部分通配符分配给打字稿中的类型?

Syl*_*ler 3 typescript

我搜索了一段时间,看看是否可能,我找不到任何东西,但也许我想要的是可能的,我只是用错误的搜索词进行搜索。

我想做的事

type a = 'String' | 'String/' + string;
const b: a = 'String/anything';
Run Code Online (Sandbox Code Playgroud)

小智 10

从 Typescript v4.1 开始运行,引入了模板文字:

type a = "String" | `String/${string}`
const b: a = "String" // Valid
const c: a = "String123" // Invalid
const d: a = "String/123" // Valid
const e: a = "String/" // Valid
Run Code Online (Sandbox Code Playgroud)

Typescript Playground 链接