typescript 타입종류

miiin_sseong·2022년 6월 14일
0

타입종류

  • String
  • Number
  • Boolean
  • Object
  • Array
  • Tuple
  • Enum
  • Any
  • Void
  • Null
  • Undefined
  • Never

String

  • 문자열인 경우
const str: string = "string";

Number

  • 숫자인 경우
const num: number = "number";

Boolean

  • boolean 경우
const bool: boolean = "boolean";

Object

  • 객체인 경우
const obj: { name: string; age: number } = {
  name: "song",
  age: 27,
};

Array

  • 배열인경우
const arr: Array<number> = [1, 2, 3];
const names: Array<string> = ["song", "minseong"];
const item: number[] = [1, 2, 3];

Tuple

  • 배열의 길이가 고정되고 각 요소의 타입을 다르게 지정하는 형식입니다.
const tuple: [string, number] = ["seoul", 5];

Enum

  • 특정 값(상수) 들의 집합을 의미합니다.
  • 인덱스 번호로도 접근이 가능합니다.
enum shoes {
  Nike,
  Adidas,
  vans,
}
const myShoes: Shoes = Shoes.Nike; // 0
const myShoes: Shoes = Shoes[0]; // Nike

Any

  • 모든 타입이 입력가능한 타입니다.
  • (개인적으로 쓸데없지만 쓸일이 많다는....)
const str: any = "hello";
const numb: any = 27;
const arr: any = ["hello", 2, true];

Void

  • 변수에는 null과 undefined에 선언하고
  • 함수의 반환값이 없을때 선언합니다.
const name: void = undefined;
function person(): void {
  console.log("exit"); // retrun X
}
profile
Github잔디를 채우기 위해 Github에서 적는중

0개의 댓글