[英]Typescript property type guards on unknown
我正在尝试输入保护未知类型
const foo = (obj: unknown) => {
if (typeof obj === 'object' && obj) {
if ('foo' in obj && typeof obj.foo === 'string') {
return obj.foo;
}
}
};
“对象”类型上不存在属性“foo”。
我也试过 is 表达式不起作用:
const foo = (obj: unknown): obj is { foo: 'string' } => {
if (typeof obj === 'object' && obj) {
if ('foo' in obj && typeof obj.foo === 'string') {
return obj;
}
}
throw new Error();
};
请考虑使用这个助手:
const hasProperty = <Obj, Prop extends string>(obj: Obj, prop: Prop)
: obj is Obj & Record<Prop, unknown> =>
Object.prototype.hasOwnProperty.call(obj, prop);
在你的情况下。 in
操作符主要与工会一起按预期工作。 请看这里,这里和这里
工作解决方案:
const hasProperty = <Obj, Prop extends string>(obj: Obj, prop: Prop)
: obj is Obj & Record<Prop, unknown> =>
Object.prototype.hasOwnProperty.call(obj, prop);
const foo = (obj: unknown) => {
if (typeof obj === 'object' && obj) {
if (hasProperty(obj, 'foo') && typeof obj.foo === 'string') {
return obj.foo;
}
}
};
但是,由于您想在obj
无效时抛出错误,因此可以使用assert function
:
const hasProperty = <Obj, Prop extends string>(obj: Obj, prop: Prop)
: obj is Obj & Record<Prop, unknown> =>
Object.prototype.hasOwnProperty.call(obj, prop);
function foo(obj: unknown): asserts obj is { foo: string } {
const isValid =
typeof obj === 'object' &&
obj &&
hasProperty(obj, 'foo') &&
typeof obj.foo === 'string';
if (!isValid) {
throw new Error();
}
};
declare var obj: unknown;
foo(obj);
obj.foo // ok
你将不得不在这里给 TypeScript 一些帮助:
type fooObj = object & { foo: unknown };
const foo = (obj: unknown) => {
if (typeof obj === 'object' && obj) {
if ('foo' in obj && typeof (obj as fooObj).foo === 'string') {
return (obj as fooObj).foo;
}
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.