TypeScriptでストリングリテラル型を定義するサンプル

2021年11月10日

環境

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Windows10 64bit
TypeScript Version 4.4.4
Windows10 64bit TypeScript Version 4.4.4
Windows10 64bit
TypeScript Version 4.4.4

書式

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
type ストリングリテラル名 =xxx
let 変数名:ストリングリテラル名
type ストリングリテラル名 =xxx let 変数名:ストリングリテラル名
type ストリングリテラル名 =xxx
let 変数名:ストリングリテラル名

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
type cft = 'study' | 'skill' | 'smart'|'hard';
let str: cft
str = 'skill';
console.log(str)
str = 'smart';
console.log(str)
type cft = 'study' | 'skill' | 'smart'|'hard'; let str: cft str = 'skill'; console.log(str) str = 'smart'; console.log(str)
type cft = 'study' | 'skill' | 'smart'|'hard';
 
let str: cft
 
str = 'skill';
console.log(str)

str = 'smart'; 
console.log(str)

結果

skill
smart

TypeScript

Posted by arkgame