반응형
250x250
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 | 31 |
Tags
- removeAll
- 팩토리패턴
- 참조형 매개변수
- 네트워크
- 자바의 면접
- 싱글톤 패턴
- k번째큰수
- 후위표기식
- SOLID원칙
- 옵저버 패턴
- mvvm패턴
- 쇠막대기
- @Tranctional
- 빈 타입 조회
- www.naver.com치면 발생하는일
- 스프링 컨테이너
- 백준 1935
- 전략 패턴
- 백준 2164
- 포워드 프록시
- 스프링 싱글톤
- 팩토리 패턴
- 기본형 매개변수
- 스프링 빈
- TCP/IP 4계층
- Class Loader
- try-catch
- 리버스 프록시
- 스프링
- 참조형 반환타입
Archives
- Today
- Total
스파이더 웹 개발
타입스크립트 - 제네릭 본문
728x90
반응형
function helloString(message: string): string{
return message;
}
function helloNumber(message: number): number{
return message;
}
function hello(message: any): any {
return message;
}
//any의 경우 input에 따라 달라지는 코드를 구현할 수 없다
console.log(hello('Mark').length);
// console.log(hello(30).length);
function helloGeneric<T>(message: T): T{
return message;
}
//generic의 경우 input에 따라 다르게 코드를 구현할 수 있다
console.log(helloGeneric('Mark').length);
console.log(helloGeneric(30).toString);
console.log(helloGeneric(true).valueOf);
제네릭 기본
function helloBasic<T>(message: T): T {
return message;
}
// helloBasic<string>(30); 컴파일에러
helloBasic<string>('Mark'); //제너릭 지정
helloBasic(36); //제너릭 추론
제네릭 배열/튜플
function helloArray<T>(message: T[]): T {
return message[0];
}
helloArray(['Hello','World']);
helloArray(["hello",5]); //string | number
function helloTuple<T,K>(message: [T,K]):T {
return message[0];
}
helloTuple(['Hello', 'World']);
helloTuple(["hello", 5]);
제네릭 클래스
class Person<T,K> {
private _name: T;
private _age: K;
constructor(name: T, age: K){
this._name = name;
this._age = age;
}
}
new Person('Mark',39);
new Person(40,30);
// new Person<String>(30); 컴파일 에러
제네릭 상속
class PersonExtends<T extends string | number> {
//T는 string과 number만 가능하다
private _name: T;
constructor(name: T){
this._name = name;
}
}
// new PersonExtends(true); 컴파일 에러
new PersonExtends('Mark');
new PersonExtends(30);
728x90
반응형
'JavaScript' 카테고리의 다른 글
자바스크립트 전체 복습(3) (0) | 2022.10.15 |
---|---|
자바스크립트 복습(2) (0) | 2022.10.14 |
자바스크립트 - 복습 (0) | 2022.10.14 |
자바스크립트 - 제네릭 (0) | 2022.10.11 |
타입스크립트 확장 (0) | 2022.10.11 |
Comments