[ 타입스크립트의 기본타입 ]
타입스크립트의 타입을 정의하는 방법은 아래와 같다.
1. boolean type
// boolean
const isTypeScriptAwesome : boolean = true;
const doesJSHasTypees : boolean = false;
2. 숫자
//number
const yourScore : number = 100;
const ieee7541IsAwesome : number = 0.1 + 0.2;
3. 문자열
//string
const authorName : string = 'name';
const toReaders : string = `
n
a
m
e`;
4. null / undefined
const nullValue : null = null;
const undefinedValue : undefined = undefined;
const numberValue : number = null; // error
5. any
- 모든 타입과 호환이 가능함, 타입검사 수행 안함.
let bool : any = true;
bool = 3;
bool = 'whatever';
6. void
- null과 undefined 만을 값으로 가질 수 있는 타입. 아무런 값도 반환하지 않는 함수의 반환타입 표
function nothing () : void {}
7. never
- 절대 존재할 수 없는 값을 암시. 어떤 값도 할당할 수 없음.
function alwaysThrow(): never {
throw new Error(`I'm a wicked function!`);
}'TypeScript Study' 카테고리의 다른 글
| TypeScript Props 타입 정의 방법, useState (0) | 2025.11.23 |
|---|---|
| TypeScript 02. 배열과 튜플과 객체 (1) | 2023.12.06 |
댓글