2024.11.05 - [TS] - TypeScript의 타입

 

TypeScript의 타입

1. 기본string: 문자열 타입let name: string = "홍길동"; number: 숫자 타입number: 숫자 타입 boolean: 참/거짓 타입let isActive: boolean = true; null 및 undefined: 각각 null과 undefined를 나타냄let value: null = null;let notAs

ysh0129.tistory.com

 

any

모든 타입 할당 가능

let thing: any = 100;
thing = "str?ing";
thing = false;
console.log(thing); // 마지막에 할당된 false 출력

주의할점으로 any를 많이 사용하면 타입스크립트의 장점을 잃을 수 있으니 남발하지 말자

 

void

⇔ any와 반대의 개념, 어떠한 타입도 존재하지 않는다.

함수에서 반환 값이 없을 때 반환 타입을 표현하기 위해 쓰인다.

function message():void {
    console.log("This is my message");
}

 

never

절대 발생 할 수 없는 타입

type Animal2 = "cat" | "dog";

function handleAnimal(animal: Animal2) {
    if (animal === "cat") {
        console.log("It's a cat!");
    } else if (animal === "dog") {
        console.log("It's a dog!");
    } else {
        const neverValue: never = animal; // 이 부분은 절대 실행되지 않음
        console.log(neverValue);
    }
}

handleAnimal("cat"); // if문 출력
handleAnimal("dog"); // else if문 출력

 

▼ 자세한 내용은 해당 사이트

https://typescript-kr.github.io/

 

TypeScript 한글 문서

TypeScript 한글 번역 문서입니다

typescript-kr.github.io

 

'TS' 카테고리의 다른 글

Interface (2)  (0) 2024.11.18
Interface (1)  (0) 2024.11.15
Interface  (0) 2024.11.08
TypeScript의 타입  (0) 2024.11.05
TypeScript란 무엇인가?  (0) 2024.11.04

+ Recent posts