다형성
type SuperPrint = {
(arr: number()):void
(arr: boolean()):void
(arr:string()):void
(arr: boolean|number()):void
}
const superPrint: SuperPrint = (arr) => {
arr.forEach(i=>console.log(i))
}
const a = superPrint((1,3,9))
const b = superPrint((‘1’,’3’,’9’))
const c = superPrint((false,3,9))
기능하다 그러므로 첨부된 매개변수에 따라서 호출 서명~이다 의 형태 다양성 가지다 수도 있다.
일반적인
그러나 매개변수에 전달된 정확한 유형을 모르는 호출 서명의 다양성을 위해.
호출 서명붓다 전체 모두 쓰다 어렵거나 모호한 시간 있다.
type SuperPrint = {
<T>(arr: T()): T
}
const superPrint: SuperPrint = (arr) => {
arr.forEach(i=>console.log(i))
}
const a = superPrint((1,3,9))
const b = superPrint((‘1’,’3’,’9’))
const c = superPrint((false,3,9))
이 경우 구체적인 유형(숫자, 문자열, 부울…) 대신 일반 유형을 사용하는 경우
Typescript에 제네릭 유형을 사용할 것이라고 알려줌으로써 Typescript는 그것이 어떤 유형인지 유추합니다.
모든 매개변수 유형에 대한 호출 서명을 쉽게 생성할 수 있습니다.
(+ 및 일반적인~에서 이름은 나 원하다 이름으로 구체화하다 숫자 있다.)
* 똑같은 것 유형에게 일반적인두번째 둘 이상 이름을 짓다 당신이 원할 때마다
type SuperPrint = {
<T,M>(arr: T(), arr2: M): T
}
const superPrint: SuperPrint = (arr) => {
arr.forEach(i=>console.log(i))
}
const a = superPrint((1,3,9), ’M’)
const b = superPrint((‘1’,’3’,’9’), ’M’)
const c = superPrint((false,3,9), ’M’)
* 일반적인두번째 쓰다 기능하다 빨리 적용될 때
function superPrint<T>(a: T()){
Return a(0)
}
const a = superPrint((1,3,9), ’M’)
const b = superPrint((‘1’,’3’,’9’), ’M’)
const c = superPrint((false,3,9), ’M’)
* 유형두번째 확장 중 일반적인
type Player<E> ={
name:string
extraInfo : E
}
Typescript가 자체적으로 유형 추론을 수행하도록 하는 것이 가장 좋습니다.
일반적인두번째 사용법을 통해 유형두번째 만들다 수도 있다 유형두번째 확장하다 수도 있다.
플레이어 입력~에서 추가 정보~에서 유형에게 다재 주어야 한다 만약에 이와 같이 얻기 쉬운 있다.
type Player<E> ={
name:string
extraInfo : E
}
Type Extra = {
favFood: ’string’
}
Type subinPlayer = Player<Extra>
Const subin: subinPlayer = {
Name: ‘subin’,
extraInfo: {
favFood: ‘kimchi’
}
}
const Lynn: Player<null> = {
name: ‘Lynn’,
extraInfo: null
}
그리고 기존 유형을 사용자 지정 일반 유형으로 변경할 수 있습니다.
이와 같이 유형두번째 만들다 그만큼. 유형두번째 다른 유형에게 세트 사용 숫자 있다.
*출처(Nomadcoder의 토크)
https://nomadcoders.co/typescript-for-beginners/lectures/3677
https://nomadcoders.co/typescript-for-beginners/lectures/3676
https://nomadcoders.co/typescript-for-beginners/lectures/3674
