프론트엔드/Typescript

Template Literal Types

.log('FE') 2021. 11. 12. 19:00
728x90
반응형
type attrs = "Name" | "Age";
type target = `get${attrs}`;
// target = getName | getAge;

// ========================== //

// bad
type CssPadding = 'padding-left' | 'padding-right' | 'padding-top' | 'padding-bottom';

// good
type Direction = 'left' | 'right' | 'top' | 'bottom';
type CssPadding = `padding-${Direction}`;
ty[e CSsMargin = `margin-${Direction};`


// ========================== //

// addName | addPhone | removeName | removePhone
type actions = 'add' | 'remove';
type property = 'name' | 'phone';

// Capitalize<StringType> : 첫 글자를 대문자로
type result = `${actions}${Capitalize<property>}`;
728x90
반응형

'프론트엔드 > Typescript' 카테고리의 다른 글

Utility Tyes - Typescript  (0) 2021.12.07