我有一个接受对象并对其进行代理的函数。
我不希望它代理类,我想用参数类型来断言。
我该怎么做?
type NonClassObject = ???;
export function createObjectProxy(obj: NonClassObject) {
return new Proxy(obj, {});
}
const foo: NonClassObject = {};
const bar: NonClassObject = {} as HTMLElement; // this should be a type error
除了构造函数的类型外,似乎没有太多区别两者。我不确定是否有办法说Exact<tyepof OBJ.constructor, Object>
...,但我现在正在研究实用程序类型。
我知道了。 object extends (any class here)
不正确。
type NonClassObject<TYPE> = Object extends TYPE ? TYPE : never;
export function createObjectProxy<TYPE extends object>(obj: NonClassObject<TYPE>) {
return new Proxy(obj, {});
}
createObjectProxy({});
createObjectProxy({} as HTMLElement); // Error: Argument of type 'HTMLElement' is not assignable to parameter of type 'never'.