This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Messenger { | |
sendText: () => void; | |
sendFile: () => void; | |
checkStatus?: () => void; | |
} | |
type RequiredFields<T> = { | |
[K in keyof T as T[K] extends Required<T>[K] ? K : never]: T[K]; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Person = { | |
name: string; | |
[key: `phone${number}`]: string; | |
}; | |
const userA: Person = { | |
name: 'John', | |
phone1: '111-111-111', | |
phone2: '222-222-222', | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Url = `https://${string}.${string}`; | |
const url1: Url = 'https://google.com'; | |
// Error: Type 'test' is not assignable to type 'https://${string}.${string}'. | |
const url2: Url = 'test'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Messenger { | |
sendText: () => void; | |
sendFile: () => void; | |
} | |
type Async<T> = { | |
[Property in keyof T as Capitalize<Property & string>]: () => Promise<void>; | |
}; | |
// type AsyncMessenger = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Messenger { | |
sendText: () => void; | |
sendFile: () => void; | |
} | |
type Async<T> = { | |
[Property in keyof T]: () => Promise<void>; | |
}; | |
// type AsyncMessenger = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface AppConfig { | |
username: string; | |
layout: string; | |
}; | |
// layout | username | |
type AppConfigKeys = keyof AppConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : any; | |
// void | |
type Example1 = ReturnType<(n: number, s: string) => void>; | |
// any | |
type Example2 = ReturnType<number>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Animal {} | |
interface Dog extends Animal {} | |
// boolean | |
type NestedExample = Dog extends Animal ? (RegExp extends Animal ? number : boolean) : string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type IsNumber<T> = T extends number ? true : false; | |
// true | |
type Example2 = IsNumber<100> | |
// false | |
type Example1 = IsNumber<RegExp> |