[英]Is it possible to use typescript mapped types to make a type of non-function properties of an interface?
so I was looking at Typescript's mapped types.所以我在看 Typescript 的映射类型。 Would it be possible to create an interface that wraps another type that removes functions from the original type?是否可以创建一个接口来包装另一种从原始类型中删除函数的类型? For example:例如:
interface Person{
name: string,
age: number,
speak(): void,
}
type Data<T> = ?
const dataPerson: Data<Person> ={
name: "John",
age: 20
//Speak removed because it is a function
};
Thanks!谢谢!
This is from the typescript documentation ( https://www.typescriptlang.org/docs/handbook/advanced-types.html#conditional-types ) and works:这是来自打字稿文档( https://www.typescriptlang.org/docs/handbook/advanced-types.html#conditional-types )并且有效:
type NonFunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T];
type Data<T> = Pick<T, NonFunctionPropertyNames<T>>;
Thanks everyone!感谢大家!
{ [K in T]: T[K] extends Function ? undefined : T[K] }
You can use a mapped conditional type for that.您可以为此使用映射的条件类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.