TS 4.1 の Template Literal Types を使うと、文字列を解釈して型定義ができる。文字列は変数化でき、テンプレートリテラルで埋め込める。 type Hello = 'Hello' type World = 'World' // Template Literal を埋め込める type HelloWorld = `${Hello}, ${World}` // => type 'Hello, World' // 引数に取ることもできる type Concat<T extends string, S extends string> = `${T}, ${S}` type HelloWorld = Concat<'Hello', 'World'> // => type 'Hello, World'
![TypeScript 4.1 の Template Literal Types がやばい - Qiita](https://cdn-ak-scissors.b.st-hatena.com/image/square/749670a82e901ae721b3f19dbfc71e645079f65a/height=288;version=1;width=512/https%3A%2F%2Fqiita-user-contents.imgix.net%2Fhttps%253A%252F%252Fcdn.qiita.com%252Fassets%252Fpublic%252Farticle-ogp-background-412672c5f0600ab9a64263b751f1bc81.png%3Fixlib%3Drb-4.0.0%26w%3D1200%26mark64%3DaHR0cHM6Ly9xaWl0YS11c2VyLWNvbnRlbnRzLmltZ2l4Lm5ldC9-dGV4dD9peGxpYj1yYi00LjAuMCZ3PTk3MiZoPTM3OCZ0eHQ9VHlwZVNjcmlwdCUyMDQuMSUyMCVFMyU4MSVBRSUyMFRlbXBsYXRlJTIwTGl0ZXJhbCUyMFR5cGVzJTIwJUUzJTgxJThDJUUzJTgyJTg0JUUzJTgxJUIwJUUzJTgxJTg0JnR4dC1jb2xvcj0lMjMyMTIxMjEmdHh0LWZvbnQ9SGlyYWdpbm8lMjBTYW5zJTIwVzYmdHh0LXNpemU9NTYmdHh0LWFsaWduPWxlZnQlMkN0b3Amcz03YTA2NzYzZWJmYmI5OGEzOWU4MTBkMzlkYTJkYjkwMQ%26mark-x%3D142%26mark-y%3D57%26blend64%3DaHR0cHM6Ly9xaWl0YS11c2VyLWNvbnRlbnRzLmltZ2l4Lm5ldC9-dGV4dD9peGxpYj1yYi00LjAuMCZoPTc2Jnc9NzcwJnR4dD0lNDBhY3JvNXBpYW5vJnR4dC1jb2xvcj0lMjMyMTIxMjEmdHh0LWZvbnQ9SGlyYWdpbm8lMjBTYW5zJTIwVzYmdHh0LXNpemU9MzYmdHh0LWFsaWduPWxlZnQlMkN0b3Amcz0yNTJlZjA4YWY5YjQwNzU3NGM0NGQyNzRkMGU2ZTE2Ng%26blend-x%3D142%26blend-y%3D486%26blend-mode%3Dnormal%26s%3D0d91f4555a6c23523545a06dcfeb0582)