LoginSignup
2
0

More than 1 year has passed since last update.

オブジェクトのvalueで定義したプリミティブだけを許可した型を作る場合

Posted at
interface User {
  name: string | null;
  age: number | null;
  isMan: boolean | null;
}

の時を定義する方法
type UserTypes: string | number | boolean | null;

type UserTypes<K extends keyof User>  = User[K]

// 例)

interface User {
  name: string | null;
  age: number | null;
  isMan: boolean | null;
}

type UserTypes<K extends keyof User>  = User[K]

const display = <K extends keyof User>(val: User[K]) => {
   console.log(val)
}

display('戸山香澄')
display (1)
display (false)
display(null)
// Argument of type 'undefined' is not assignable to parameter of type 'string | number | boolean | null'.
display(undefined)

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0