打字稿<t extend =“” u =“ U”>语法

时间:2018-08-19 19:17:34

标签: typescript react-router

我正在看react-router中的打字稿类型,并看到了:

export class Route<T extends RouteProps = RouteProps> extends React.Component<T, any> { }

定义的RouteProps = RouteProps部分是什么意思?在文档中哪里可以找到这个?

1 个答案:

答案 0 :(得分:1)

您可以在Typescript Generics上找到文档。

此语法表示:

  1. T必须从RouteProps扩展并且

  2. 您可以使用提供泛型的此类:

示例:

interface IRoute1 extends RouteProps { }
interface IRoute2 extends RouteProps { 
    prop: string;
}
interface IOther { }

const route = new Route();
const route1 = new Route<IRoute1>();
const route2 = new Route<IRoute2>();
const routeOther = new Route<IOther>(); //this will throw an error as IOther is not extended from RouteProps

如果您不提供通用接口,它将使用RouteProps

如果提供,则必须从RouteProps开始扩展。

相关问题