타입스크립트 type

js·2021년 9월 5일
0

타입스크립트

목록 보기
2/4

기본 타입

string, number, boolean, symbol, enum, 문자열 리터럴

1) symbol : unique한 속성으로 주로 객체의 key로 사용된다

let hello = Symbol("hello");
let hello2 = Symbol("hello");
console.log(hello === hello2); // false

2) enum : 숫자형, 문자형 이넘이 있다

enum Direction {
  Up, // 0
  Down, // 1
  Left, // 2
  Right // 3
}

enum Direction {
    Up = "UP",
    Down = "DOWN",
    Left = "LEFT",
    Right = "RIGHT",
}

객체 타입

array, tuple, function, 생성자, class, interface

1) tuple

let tuple: [string, number];
tuple = ['hello', 10]; // OK

2) function

function split(str: string): string[] {
  return str.split('');
}
let myFn: (string) => string[] = split;

기타 타입

1) union : 여러개 중 하나 (유사 or)

const x: string | number | boolean;

2) intersection : 두타입을 하나로

interface Cat { leg: number; }
interface Bird { wing: number; }
let birdCat: Cat & Bird = { leg: 4, wing:2 };

3) void : 노 리턴

4) null, undefined 타입 => tsconfig.json의 strictNullChecks 옵션 으로 할당 방지 가능

  • 사용자 정의 타입
type EventType = "mouseover" | "keydown";

5) non-nullable: null, undefined 허용하지 않는 타입

6) lookup type - keyof : keyof로 속성을 포함하는 대상을 찾아서 union type처럼 동작

interface exLookup { ex1: number; ex2: number; ex3: number; } 
type useLookup = keyof exLookup;
let real:useLookup = "ex2" // 허용

0개의 댓글