TypeScript ちょっとした疑問
{} もしくは {hoge: true} の型を考えてみた。
以下を思いついたのだが、
type T1 = {hoge: true} | Record<string, never>
普通に以下のようにやった方がシンプルかもしれない
type T2 = {hoge?: true}
type T1 = {hoge: true} | Record<string, never>
type T2 = {hoge?: true}
const a1: T1 = {hoge: true}
const a2: T2 = {hoge: true}
const b1: T1 = {}
const b2: T2 = {}
//以降はコンパイルエラー
const c1: T1 = {hoge: false}
const c2: T2 = {hoge: false}
const d1: T1 = {hogee: true}
const d2: T2 = {hogee: true}
const e1: T1 = {hoge: true, foo: true}
const e2: T2 = {hoge: true, foo: true}
const f1: T1 = null
const f2: T2 = null
const g1: T1 = undefined
const g2: T2 = undefined
最近のコメント