考虑以下 TS 定义:
type GenericPropsWithChildren<T> = T & { children?: ReactNode };
没有什么问题type
但我想知道是否有一个interface
相等的?显然可以传递泛型向下进入接口,尽管这不是我所追求的,例如。 G。:
interface GenericPropsWithChildren<T> {
children?: ReactNode;
myProps: T; // not desired
}
这里的例子是在 React 代码的上下文中,但根本问题是基本的 TS。
接口的成员必须在声明时已知,在扩展中使用泛型类型参数违反了此规则。因此无法创建等效的通用接口。
如果您已经知道类型参数,您实际上可以在接口中继承类型别名。只要在声明时知道所有成员,就允许:
import { ReactNode } from 'react'
type GenericPropsWithChildren<T> = T & { children?: ReactNode };
interface Concrete extends GenericPropsWithChildren<{ p: string }> {
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)