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
- 개발자
- PostgreSQL
- 열공하자
- Partition
- ServerSentEvent
- 실시간알림
- node.js
- 성장기
- partitioning
- node.js란
- mariadb
- 파티셔닝
- localStorage
- Lag
- SSE
- frontend
- EventSource
- Node.js기본
Archives
- Today
- Total
써치킴의 우당탕탕 개발 블로그
[TypeScript][ch7][Generic] Generic Basic 본문
function helloBasic<T>(message: T): T {
return message;
}
helloBasic 함수를 사용하는 2가지 방식이 있다.
1. 타입을 지정하는 방식
- 지정한 타입으로만 인수로 입력할 수 있다.
// 1. 함수를 사용할 때 타입을 지정하는 방식 > 지정한 타입으로만 입력할 수 있게 제한이 됨
helloBasic<string>('Searchkim');
// helloBasic<string>(29); // Error 발생
- 함수 호출 시, 인수가 string형이 아니면 Error 발생
2. 타입을 지정하지 않는 방식
// 2. 타입을 지정하지 않는 방식
helloBasic('Searchkim'); // Searchkim이라는 값에 의해서 타입이 추론됨
helloBasic(29); // 29이라는 값에 의해서 타입이 추론됨
- 타입은 Searchkim이다.
- 타입은 29이다.
함수 선언 시, 타입을 여러 개 지정할 수 있다!
// 함수 선언 시, 타입을 여러 개 지정할 수 있음
function helloS<T, U>(message: T, comment: U): T {
return message;
}
// 1. 타입을 지정하는 방식
helloS<string, number>('Searchkim', 29);
// 2. 타입을 지정하지 않는 방식
helloS(26, 29); // 타입은 26, 29이다.
'TypeScript 합시다' 카테고리의 다른 글
[TypeScript][ch7][Generic] Generic Function (0) | 2022.02.18 |
---|---|
[TypeScript][ch7][Generic] Generic Array & Tuple (0) | 2022.02.18 |
[TypeScript][ch7][Generic] Generic, Any와 다른 점 (0) | 2022.02.18 |
[TypeScript][ch6][Class] 추상 클래스(Abstract Classes) (0) | 2022.02.15 |
[TypeScript][ch6][Class] 상속(Inheritance) (0) | 2022.02.15 |
Comments