Skip to content

Instantly share code, notes, and snippets.

View wolak041's full-sized avatar

Kamil Wolak wolak041

View GitHub Profile
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];
};
type Person = {
name: string;
[key: `phone${number}`]: string;
};
const userA: Person = {
name: 'John',
phone1: '111-111-111',
phone2: '222-222-222',
};
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';
interface Messenger {
sendText: () => void;
sendFile: () => void;
}
type Async<T> = {
[Property in keyof T as Capitalize<Property & string>]: () => Promise<void>;
};
// type AsyncMessenger = {
interface Messenger {
sendText: () => void;
sendFile: () => void;
}
type Async<T> = {
[Property in keyof T]: () => Promise<void>;
};
// type AsyncMessenger = {
interface AppConfig {
username: string;
layout: string;
};
// layout | username
type AppConfigKeys = keyof AppConfig;
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>;
interface Animal {}
interface Dog extends Animal {}
// boolean
type NestedExample = Dog extends Animal ? (RegExp extends Animal ? number : boolean) : string;
type IsNumber<T> = T extends number ? true : false;
// true
type Example2 = IsNumber<100>
// false
type Example1 = IsNumber<RegExp>