interface 定义函数 这块 概念我没懂

来源:2-16 泛型中使用 extends 和 keyof 语法

躲躲藏藏

2020-12-10

interface interfaceA {
  (x: string): string  // interfaceA 参数 x string类型,返回string 类型。
}

const say: interfaceA = (x: string) => { 
// 为啥 interfaceA 都定义参数类型了,此处调用时 还要在 给x 设置string 类型呢?
  return x
}

写回答

1回答

Dell

2020-12-12

正式因为interface 要求函数的参数得是string,所以你函数必须要按照他的要求来定义数据类型

0
0

专为小白设计的TypeScript入门课

Dell老师专为TypeScript小白打造的,全栈式教学TS入门课程

2249 学习 · 506 问题

查看课程