如何删除在TypeScript或Flow中未定义的继承类型的联合类型细化
我有一个从库中继承并导入的类型,我想知道如何删除它对null或未定义值的允许如何删除在TypeScript或Flow中未定义的继承类型的联合类型细化,typescript,flowtype,Typescript,Flowtype,我有一个从库中继承并导入的类型,我想知道如何删除它对null或未定义值的允许 type Foo = { baz: string } // Bar type is inherited and I would like to kill the union with undefined so I expect foo to never be falsey. type Bar = { foo?: Foo } const bar: Bar = {foo: {baz: 'baz'}}; /
type Foo = {
baz: string
}
// Bar type is inherited and I would like to kill the union with undefined so I expect foo to never be falsey.
type Bar = {
foo?: Foo
}
const bar: Bar = {foo: {baz: 'baz'}};
// this destructuring issues an error because it allows for the possibility of it being undefined and undefined can't be destructured. And I can't conditionally exit since I'm using React hooks and I'd be violating the hooks should not be used conditionally rule
const {baz} = bar.foo;
内置的typescript中有一个实用程序类型,名为,您可以像下面那样使用它
类型Foo={
baz:字符串
}
类型栏={
福?:福
}
StrictBar类型=必需
//类型StrictBar={foo:foo}
太棒了!是否有类似的流动工具?这个问题实际上是一个流程问题,我希望有一个与工具无关的解决方案。再次感谢!在看到链接后,我在这里看到了流的等价物: