背景
尴尬的库类型:
declare type Extension = {
extension: Extension;
} | readonly Extension[];
铅字保护装置
export function isIterable(x: any): x is Iterable<unknown> {
return Symbol.iterator in x;
}
什么有效
使用类型保护
const extensions:Extension = getExtensions();
if (!isIterable(extensions)) { // Guard
throw Error('extensions should be iterable');
}
console.log(...extensions); // Works
问题
我正在尝试创建一个 * 类型保护辅助函数 *:
import { isIterable } from '../type-guarded/isIterable';
export const throwIfNotIterable = <T,>(value: T) => {
if (isIterable(value)) {
return value;
}
throw TypeError(
`Expected to be iterable, instead got ${value} (${typeof value})`
);
};
注:推断类型:
const throwIfNotIterable: <T>(value: T, name: string) => T & Iterable<unknown>;
尝试次数
尝试1
console.log(...throwIfNotIterable(extensions));
错误代码:
Type 'unknown' is not assignable to type 'Extension'.
尝试2
throwIfNotIterable(extensions, 'not iterable');
console.log(...extensions);
错误代码:
Type 'Extension | undefined' must have a '[Symbol.iterator]()' method that returns an iterator.
我哪里做错了?
1条答案
按热度按时间jtw3ybtb1#
可以将
throwIfNotIterable
设置为Assert函数(也可以将其名称改为assertIsIterable
,因为Assert函数通常以assert
开头):Playground示例