string literal type으로 오직 하나의 string literal만 받는 type 만들 수 있음.
아래는 'click'만 받음.
let click: 'click';
click = 'click'; // valid
click = 'dblclick'; // compiler error
아래 처럼 union type이랑 같이 써서 변수에 들어갈 string set을 정하기도 좋음.
let mouseEvent: 'click' | 'dblclick' | 'mouseup' | 'mousedown';
mouseEvent = 'click'; // valid
mouseEvent = 'dblclick'; // valid
mouseEvent = 'mouseup'; // valid
mouseEvent = 'mousedown'; // valid
mouseEvent = 'mouseover'; // compiler error
string literal type을 여러군데서 써야하면 아래처럼 type alias로 코드 가독성 높이는게 좋음.
type MouseEvent: 'click' | 'dblclick' | 'mouseup' | 'mousedown';
let mouseEvent: MouseEvent;
mouseEvent = 'click'; // valid
mouseEvent = 'dblclick'; // valid
mouseEvent = 'mouseup'; // valid
mouseEvent = 'mousedown'; // valid
mouseEvent = 'mouseover'; // compiler error
let anotherEvent: MouseEvent;
string literal type은 특정한 string literal만 받는 type임.
union tpye이랑 type alias랑 같이써서 특정한 문자열 집합만 받는 type만들 수 있음.