Skip to content

Instantly share code, notes, and snippets.

@shisama
Last active June 9, 2019 05:34
Show Gist options
  • Save shisama/9edc4e0147936d1bd5af70932dd02d90 to your computer and use it in GitHub Desktop.
Save shisama/9edc4e0147936d1bd5af70932dd02d90 to your computer and use it in GitHub Desktop.
Advanced built-in types you may have never used
type SupportedDomains = "google.com" | "amazon.com" | "google.co.jp" | "amazon.co.jp"
type USDomains = "google.com" | "amazon.com" | "facebook.com"
// "google.com" | "amazon.com"
type SupportedUSDomains = Extract<SupportedDomains, USDomains>
// "google.co.jp" | "amazon.co.jp"
type UnsupportedUSDomains = Exclude<SupportedDomains, USDomains>
const domain1: SupportedDomains = "google.co.jp"
const domain2: Extract<SupportedDomains, USDomains> = "google.com"
// Compile Error because value is not US domain
const domain3: Extract<SupportedDomains, USDomains> = "google.co.jp"
const domain4: Exclude<SupportedDomains, USDomains> = "google.co.jp"
// Compile Error because value is US domain
const domain5: Exclude<SupportedDomains, USDomains> = "google.com"
// make "strictNullChecks" option true
type NullableDomin = "google.com" | "amazon.com" | null | undefined
// "google.com" | "amazon.com
type NonNullableDomin = NonNullable<NullableDomin>
const domain1: NullableDomin = null
const domain2: NullableDomin = undefined
// Compile Error: Type 'null' is not assignable to type '"google.com" | "amazon.com"'.
const domain3: NonNullable<NullableDomin> = null
// Compile Error: Type 'undefined' is not assignable to type '"google.com" | "amazon.com"'.
const domain4: NonNullable<NullableDomin> = undefined
type ExtractName = {
name: string
}
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
type Diff<T, K> = Omit<T, keyof K>;
function removeName<Props extends ExtractName>(
props: Props
): Diff<Props, ExtractName> {
const { name, ...rest } = props;
return rest;
}
function linearSearch(a: Array<number>, x: number) {
for (let i = 0; i < a.length; i++) {
if (a[i] === x) {
return i;
}
}
return null;
}
// (a: number[], x: number) => number
type Fn = typeof linearSearch;
// [number[], number]
type Params = Parameters<Fn>
// Compile Success
const params1: Params = [[1, 2, 3], 3]
// Compile Error: Type 'number' is not assignable to type 'string'.
const params2: Params = [[1, 2, 3], '3']
type User = {
id?: number,
name: string
};
type PartialUser = Partial<User>
/*
{
id?: number,
name?: string
}
*/
// Compile Error: Property 'id' is missing in type '{}' but required in type 'User'.
const user1: User = {}
// Compile Success
const user2: Partial<User> = {}
type User = {
id?: number,
name: string
};
type PickedUser = Pick<User, "id">
/*
{
id?: number
}
*/
const user1: User = {
id: 1,
name: "taro"
}
// Compile Error: 'name' does not exist in type 'Pick<User, "id">'
const user2: Pick<User, "id"> = {
id: 2,
name: "hanako"
}
// Compile Success
const user3: Pick<User, "id"> = {
id: 3
}
type User = {
id?: number,
name: string
};
type ReadonlyUser = Readonly<User>
/*
{
readonly id?: number,
readonly name: string
}
*/
const user1: User = {
name: "taro"
}
user1.name = "hanako"
const user2: Readonly<User> = {
name: "ichiro"
}
user2.name = "jiro" // Compile Error
type Last = 'sato' | 'suzuki' | 'tanaka'
type First = {[key: string]: string}
// Compile Error: Property 'tanaka' is missing
const names: Record<Last, First[]> = {
sato: [{ name: 'taro' }, { name: 'hanako' }],
suzuki: [{ name: 'ichiro' }],
}
names.sato
names.tanaka
type Last = 'sato' | 'tanaka'
type First = { [key: string]: string }
type NameRecord = Record<Last, First[]>
/*
{
sato: First[],
tanaka: First[]
}
*/
const names: Record<Last, First[]> = {
sato: [{ name: 'taro' }, { name: 'hanako' }],
suzuki: [{ name: 'ichiro' }], // Compile Error
}
names.sato
type User = {
id?: number,
name: string
};
type RequiredPropsUser = Required<User>
/*
{
id: number,
name: string
}
*/
const user1: User = {
name: 'taro'
}
// Compile Error because id is required
const user2: Required<User> = {
name: 'hanako'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment