Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kenmori/948d69efc0a269565209a0da97150aaa to your computer and use it in GitHub Desktop.
Save kenmori/948d69efc0a269565209a0da97150aaa to your computer and use it in GitHub Desktop.
Type that has null able and optional but function type is require

Type that has null able and optional, but function type is require.

type NullAbleType<T> = {[K in keyof T]: T[K] | null}
type User = {name: string, age: number, money: null, fn: (...arg: any[])=> void}
type FunctionType<T> = {[K in keyof T]: T[K] extends Function? T[K] : never}[keyof T]
type FunctionName = "fun"
type NullAbleUserAndRequireFunction<T> = NullAbleType<Partial<User>> & {[K in T extends FunctionName ? T: never]: FunctionType<User>}

const obj:NullAbleUserAndRequireFunction<FunctionName> = {name: null, age: 99, money: null, fun: ()=>{}}

playground

If FunctionType allows null, more simple

type NullAbleType<T> = {[K in keyof T]: T[K] | null}
type User = {name: string, age: number, money: null, fn: (...arg: any[])=> void}

const obj:NullAbleType<Partial<User>> = {name: null, age: 99, money: null, fn: ()=>{}}

playground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment