December 23, 2020
대체로 타입변수는
T
, 또 다른 타입변수는U
, 속성은K
로 한다.
좀더 직관적으로 하기 위해 또 다른 타입변수는T2
로 카운팅하겠음
?
)로 반환
<T>
interface IUser {
name : string
age : number
}
↓ // 변환
interface PartialUser {
name? : string
age? : number
}
const userA : IUser = { // 'age' 속성이 '{ name: string; }' 형식에 없지만 'IUser' 형식에서 필수입니다.ts(2741)
name : 'A'
}
const userB : Partial<IUser> = {
name : 'B'
}
?
를 제거)
<T>
interface IUser {
name? : string
age? : number
}
↓ // 변환
interface RequiredUser {
name : string
age : number
}
const userA : IUser = {
name : 'A'
}
const userB : Required<IUser> = { // 'age' 속성이 '{ name: string; }' 형식에 없지만 'Required<IUser>' 형식에서 필수입니다.
name : 'B'
}
<T>
interface IUser {
name? : string
age? : number
}
↓ // 변환
interface ReadonlyUser {
readonly name : string
readonly age : number
}
const userA : IUser = {
name : 'A',
age : 12
}
const userB : Readonly<IUser> = {
name : 'B',
age : 13
}
userB.name = 'C' // 읽기 전용 속성이므로 'name'에 할당할 수 없습니다.ts(2540)
Type Aliases
의 타입들을 속성으로 갖는 인터페이스를 만드는데 사용하는듯 함.
<K, T>
type User = 'sangmin' | 'gildong'
const user : Record<User, string> = {
sangmin : 'frontend',
gildong : 'backend'
}
↓ // 변환
interface RecordUser {
sangmin : string
gildong : string
}
Type은 속성을 가지는 인터페이스나 객체여야 함
<T, K>
interface IUser {
name : string,
age : number,
email : string,
isValid : boolean
}
type TKey = 'name' | 'age'
const user : Pick<IUser, TKey> = {
name : '상민',
age : 26
email : 'sangmin802@naver.com' // 개체 리터럴은 알려진 속성만 지정할 수 있으며 'Pick<IUser, TKey>' 형식에 'email'이(가) 없습니다.ts(2322)
}
↓ // 변환
interface PickUser {
name : string
age : number
}
Type은 속성을 가지는 인터페이스나 객체여야 함
<T, K>
Pick
과 반대로, 기존의 틀에서 지정한 속성들을 제외한 새로운 인터페이스를 생성 interface IUser {
name : string,
age : number,
email : string,
isValid : boolean
}
type TKey = 'name' | 'age'
const user : Omit<IUser, TKey> = {
age : 26, // 개체 리터럴은 알려진 속성만 지정할 수 있으며 'Pick<IUser, "email" | "isValid">' 형식에 'age'이(가) 없습니다.ts(2322)
email : 'sangmin802@naver.com',
isValid : true
}
↓ // 변환
interface OmitUser {
email : string,
isValid : boolean
}
<T1, T2>
type T = string | number;
const a : Exclude<T ,number> = 1 // 'number' 형식은 'string' 형식에 할당할 수 없습니다.
const b : Exclude<T ,number> = '문자열'
↓ // 변환
type ExcludeT = string;
서로 공통적으로 가지고 있는 타입만 반환하는듯 함
<T1, T2>
type T = string | number;
type U = number | boolean;
const a : Extract<T ,U> = 123
const b : Extract<T ,U> = false // 'boolean' 형식은 'number' 형식에 할당할 수 없습니다.ts(2322)
const c : Extract<T ,U> = '문자열' // 'string' 형식은 'number' 형식에 할당할 수 없습니다.ts(2322)
<T>
type T = string | number | undefined | null;
const a : T = null
const b : NonNullable<T> = null // Type 'null' is not assignable to type 'string | number'.
// strictNullChecks": true 사용시 null을 다른타입에서 더이상 인정하지 않아 에러 발생함
<T>
function func(a : string | number, b : boolean){
return `[${a}, ${b}]`
}
const a : Parameters<typeof func> = [123, true]
const b : Parameters<typeof func> = ['문자열', 123] // 'number' 형식은 'boolean' 형식에 할당할 수 없습니다.ts(2322)
<T>
class User {
constructor(public name : string, public ace : number){}
}
const a : ConstructorParameters<typeof User> = ['상민', 26]
const b : ConstructorParameters<typeof User> = ['상민'] // 소스에 1개 요소가 있지만, 대상에 2개가 필요합니다.ts(2322)
반환되는 값의 타입을 다른 함수나 타입을 통해 동적으로 해야 할 때 쓸 듯?
<T>
function func(a : string){
return a;
}
const a : ReturnType<typeof func> = '문자열';
function func2(a : string) : ReturnType<() => number> {
return a.length;
}
잘.. 모르겠음
<T>
class User {
constructor(public name : string){}
}
const user : InstanceType<typeof User> = new User('sangmin');
↓ // 변환
const instanceTypeUser : User = new User('sangmin');
unknown
타입 반환returnType
은 함수의 반환타입을 타입으로 사용한다면, ThisParameterType
는 함수의 this
로 명칭된 매개변수의 타입을 가져와서 사용한다.그로인해 해당 유틸리티의 타겟이 된 함수에 인자를 보내고자할 때에는,
apply
를 사용해야한다.
<T>
function toHex(this : number) : string {
return this.toString(16)
}
function numberToString(n : ThisParameterType<typeof toHex>){
return toHex.apply(n)
}
용도를 잘 모르겠음…
* 대표타입 : 함수
* 타입변수 : `<T>`
const cat = {
age : 12
}
const dog = {
age : '12'
}
function getAge(this : typeof cat) {
return this.age
}
getAge.apply(cat)
getAge.apply(dog) // Argument of type '{ age: string; }' is not assignable to parameter of type '{ age: number; }'.
const getAgeForDog : OmitThisParameter<typeof getAge> = getAge;
getAgeForDog.apply(dog);
타입스크립트버전 apply인가..?
<T>
user
의 인자로 보내지는 introduce
내부 this.name
은 에러가 발생함ThisType<IUser>
를 통해서, IUser
영역을 잡아줌.this.name
에서 this
는 IUser
인터페이스를 바라보게 됨ThisType
유틸리티는 아무런 타입도 반환하지 않기 때문에, 해당 함수의 리턴값이 정상적으로 추론되지 않음.as IUser
와 같은 타입단언으로 리턴값의 타입을 지정해줌interface IUser {
name : string
introduce : () => string
}
function makeUser(methods : ThisType<IUser>){
return {name : '상민', ...methods} as IUser;
}
const user = makeUser({
introduce(){
return this.name;
}
});
user.introduce(); // '상민'