LoginSignup
0
0

More than 3 years have passed since last update.

TypeScript 引数に分割代入を使った時の型定義

Last updated at Posted at 2019-12-11

デフォルト値のある分割代入をした時に書き方がわからなくて詰まったのでメモ

はじめは分割代入の型を必須{ hoge: number }として書いていたせいでプロパティがない!って言われていたけど
オプション{ hoge?: number }にしてあげればよし
よく考えれば当たり前だった

class MyClass {
  hoge: number

  public constructor({ hoge = 1 }: { hoge?: number } = {}) {
    this.hoge = hoge
  }
}

const m1 = MyClass()
const m2 = MyClass({hoge: 1})
0
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
0
0