typescript - Typescript 在函数之前或类型声明之后编写的基因有什么区别?
问题描述
箭头函数的两种类型声明有什么区别?
export type Sort = <D>(r: Rows<D>, f: Field<D>, o: Order) => Rows<D>;
export type Sort<D> = (r: Rows<D>, f: Field<D>, o: Order) => Rows<D>;
解决方案
功能方面没有区别,TS 编译器会强制你在不同的地方声明 D:
https://stackblitz.com/edit/typescript-yb3pjn?file=index.ts
interface Foo {
valX: number
}
interface Bar {
valY: string
}
interface Rows<D> {
rows: D[]
}
interface Field<D> {
value: D
}
type Order = 'asc' | 'desc'
export type SortA = <D>(r: Rows<D>, f: Field<D>, o: Order) => Rows<D>;
export type SortB<D> = (r: Rows<D>, f: Field<D>, o: Order) => Rows<D>;
const sortFunction: SortA = <Foo>(rows: Rows<Foo>, field: Field<Foo>, o: Order) => {
return rows
}
const sortBFunction: SortB<Bar> = (rows: Rows<Bar>, field: Field<Bar>, o: Order) => {
return rows
}
推荐阅读
- python - 我的旧笔记本电脑上有 python 3.7.3,我在新笔记本电脑上安装了 python 3.8,但它不再工作了
- android - 颤振中的持久变量
- python - 如何使用 python 解压缩/提取 tgz.part-af 文件?
- c++ - 对于其类声明之外的静态成员初始化,是否有必要提供其类型?
- node.js - 即使路线存在,Express 也会返回 404
- jmeter - 从 JMeter 测试计划中获取最大并发并限制其值
- javascript - 使用 Angular 和 Ionic 突出显示所选项目
- ios - 与父 UIView 容器的安全区域相比,如果您创建相对于父 UIView 容器本身的约束,是否有任何区别?
- java - 单线程调度执行服务
- laravel - For 循环无法与 jQuery axois 函数一起正常工作