반응형
1. type 키워드 & readonly
//type alias
type Animal = string | number | undefined;
let animal: Animal = 123;
type UserType = { name: string; age: number };
let user: UserType = { name: 'yohan', age: 30 };
//타입스크립트내에서는 readonly를 사용하면 수정이 불가하다.
type Name = { readonly name: string };
const man: Name = {
name: 'yo',
};
type Age = string;
type City = number;
//union type으로 합치기 또한 가능하다
type Person = Name | City;
// &연산자로 object 타입 합치기
type PositionX = { x: number };
type PositionY = { y: string };
type NewType = PositionX & PositionY;
let position: NewType = { x: 123, y: '123' };
//주의사항 type변수 재정의 불가능.
2. Literal Types로 만드는 const 변수 유사품
//1. 더 엄격한 타입지정 가능
//2. 자동완성 힌트를 줄 수 있다.
let names: 'kim';
names = 'kim';
let me: 'man' | 'hi';
me = 'hi';
function a(x: '가위' | '바위' | '보'): ('가위' | '바위' | '보')[] {
return ['보'];
}
a('가위');
반응형
'TypeScript' 카테고리의 다른 글
interface 사용법 (0) | 2022.05.13 |
---|---|
타입스크립트로 HTML 변경과 조작할 때 주의점들 (0) | 2022.05.12 |
타입 확정하기 Narrowing & Assertion (0) | 2022.05.11 |
union type, any, unknown, 함수에 타입 지정하는 법 & void 타입 (0) | 2022.05.11 |
primitive types 설정법 (0) | 2022.05.11 |