Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
Tags
- 이터러블프로토콜
- tts-1
- 자바객체 #자바인스턴스 #객체와인스턴스차이점
- DestructuringAssignment
- iterationprotocol
- Realtime
- 바닐라JS #바닐라JS로크롭앱만들기 #
- gpt-4o-transcribe
- flex사용법
- AI
- ime일본어처리
- ES6란
- JAVA #Swing #자바스윙 #스윙
- ime란
- flex특징
- 자바스크립트sort함수
- realtimeapi
- flexgrid차이점
- whisper-1
- 배열재정렬함수
- 자바스크립트filter
- chatGPT
- 자바스크립트문법 #노마드코더 #
- interable
- 이터레이터프로토콜
- OpenAI
- javascript문법
- 이터레이션프로토콜
- 레이아웃모델
- 음성채팅
Archives
- Today
- Total
끄적끄적 개발기록
[타입스크립트] Void, Never , Union , Intersection ,Function 본문
Void
void는 일반적으로 값을 반환하지 않는 함수에서 사용
값을 반환하지 않는 함수는 undefined를 반환한다
void위치는 함수가 반환 타입을 명시하는 곳
function hello(msg: string): void {
console.log(`Hello ${msg}`);
}
const hi: void = hello('world'); // Hello world
console.log(hi); // undefined
// Error - TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
function hello(msg: string): undefined {
console.log(`Hello ${msg}`);
}
Never
절대 발생하지 않을 값을 의미, 어떠한 타입에도 적용 불가
function error(message: string): never {
throw new Error(message);
}
언제 사용할까?
빈 배열을 타입으로 잘못 선언한 경우 Never로 볼 수 있다!!
const never: [] = [];
never.push(3); // Error - TS2345: Argument of type '3' is not assignable to parameter of type 'never'.
Union
2개 이상의 타입을 허용하는 경우 유니언이라고 한다.
vertical bar( | )를 통해 타입을 구분 ( )는 선택사항임
let union: (string | number);
union = 'Hello type!';
union = 123;
union = false; // Error - TS2322: Type 'false' is not assignable to type 'string | number'.
**or과 같은 너낌
Intersection
&를 이용하여 2개 이상의 타입을 조합하는 경우 이를 인터섹션이라 한다.
인터섹션은 새로운 타입을 생성하지 않고 기존의 타입들 조합 가능
but 잘 사용하지는 않음
**and와 같은 너낌
// 기존 타입들이 조합 가능하다면 인터섹션을 활용할 수 있습니다.
interface IUser {
name: string,
age: number
}
interface IValidation {
isValid: boolean
}
const heropy: IUser = {
name: 'Heropy',
age: 36,
isValid: true // Error - TS2322: Type '{ name: string; age: number; isValid: boolean; }' is not assignable to type 'IUser'.
};
const neo: IUser & IValidation = {
name: 'Neo',
age: 85,
isValid: true
};
// 혹은 기존 타입(IUser, IValidation)과 비슷하지만, 정확히 일치하는 타입이 없다면 새로운 타입을 생성해야 합니다.
interface IUserNew {
name: string,
age: number,
isValid: boolean
}
const evan: IUserNew = {
name: 'Evan',
age: 36,
isValid: false
};
Function
화살표 함수를 이용하여 타입을 지정할 수 있다.
// myFunc는 2개의 숫자 타입 인수를 가지고, 숫자 타입을 반환하는 함수.
let myFunc: (arg1: number, arg2: number) => number;
myFunc = function (x, y) {
return x + y;
};
// 인수가 없고, 반환도 없는 경우.
let yourFunc: () => void;
yourFunc = function () {
console.log('Hello world~');
};
출처
goorm
구름은 클라우드 기술을 이용하여 누구나 코딩을 배우고, 실력을 평가하고, 소프트웨어를 개발할 수 있는 클라우드 소프트웨어 생태계입니다.
www.goorm.io
'개발 > Typescript' 카테고리의 다른 글
| [타입스크립트]인터페이스 (0) | 2022.04.08 |
|---|---|
| [타입스크립트]타입 추론(Inference)과 타입 단언(Assertions) (0) | 2022.04.06 |
| [타입스크립트] Void, Never , Union , Intersection ,Function (0) | 2022.04.06 |
| [타입스크립트] Unknown, Object , Null, Undefined (0) | 2022.04.06 |
| [타입스크립트] Tuple, Enum , Any (0) | 2022.04.06 |