1
GentleFifth 2023-09-01 20:55:01 +08:00 via Android
可以用 union type
|
2
lsy99 2023-09-05 16:01:01 +08:00
楼上+1 , 用 type 的联合类型
type Layout = { hasPadding: false } | { hasPadding: true; padding: string }; // correct const layout1: Layout = { hasPadding: true, padding: "12px" }; const layout2: Layout = { hasPadding: false }; // wrong //@ts-expect-error const layout3: Layout = { hasPadding: true }; //@ts-expect-error const layout4: Layout = { hasPadding: false, padding: "12px" }; |