Reactjs 如何定义可能未定义的类型
定义可能未定义的接口的最佳方法是什么?我有的是这个。我在寻找另一种选择。如果可能的话,更优雅简洁的东西Reactjs 如何定义可能未定义的类型,reactjs,typescript,Reactjs,Typescript,定义可能未定义的接口的最佳方法是什么?我有的是这个。我在寻找另一种选择。如果可能的话,更优雅简洁的东西 interface RouteInterface { path: string; test: boolean; } type TypeOrUndefined<T> = T | undefined; 接口路由接口{ 路径:字符串; 测试:布尔; } 类型TypeOrUndefined=T |未定义; 我就是这样使用它的: const returnObj: TypeOrUn
interface RouteInterface {
path: string;
test: boolean;
}
type TypeOrUndefined<T> = T | undefined;
接口路由接口{
路径:字符串;
测试:布尔;
}
类型TypeOrUndefined=T |未定义;
我就是这样使用它的:
const returnObj: TypeOrUndefined<RouteInterface> =
redirectChoices.find(
(option: RouteInterface) => option.test
);
const returnObj:TypeOrUndefined=
重定向选择。查找(
(选项:RouteInterface)=>option.test
);
通常我个人会这样做:
const returnObj: RouteInterface | undefined =
redirectChoices.find(
(option: RouteInterface) => option.test
);
const returnObj =
redirectChoices.find(
(option: RouteInterface) => option.test
);
此外,此特定代码也可以这样编写:
const returnObj: RouteInterface | undefined =
redirectChoices.find(
(option: RouteInterface) => option.test
);
const returnObj =
redirectChoices.find(
(option: RouteInterface) => option.test
);
returnObj
仍然是RouteInterface |未定义的
,因为Array.prototype.find
返回包含未定义的
的联合