Comments (3)
type A<T> = (x: T) => T
const fn1: A<string> = (x) => x
fn1('hello')
type B = <T>(x: T) => T;
const fn2: B = x => x;
fn2<string>('world')
fn2(17)
fn2(null)
A
是一个泛型,用于生成一个新的类型,使用 A<Type>
语法来生成新的类型。
B
是一个函数签名,<T>
可以在调用函数的时候传入,也可以省略,TS 会根据参数来进行类型推导。
个人理解大概就是这样吧,名词用得可能不十分准确。
from fe-interview.
结论
- type A 声明一个泛型
- type B 声明一个非泛型
解释
type A
type A 声明一个泛型,也就是说你可以:
A<string>
A<number>
...
这样使用,因为它是一个泛型。而且你必须这么用,比如如下代码是不合法的:
const identity3:A = (x) => {
return x
}
这样才合法:
const identity3:A<string> = (x) => {
return x
}
type A<T = 默认类型>
但是我可以这样声明一个泛型:
type A<T = string> = (x:T) => T;
这个时候是可以的:
const identity3:A = (x) => {
return x
}
type B = (x:T) => T;
type B 声明一个非泛型,也就是说你不能像使用泛型一样使用它:
const identity5:C<string> = (x) => {
return x
}
你只能:
const identity5:C = (x) => {
return x
}
这会造成以下代码报错,尽管逻辑上似乎行得通:
const identity5:C = (x:string) => {
return '1'
}
TS 4.0.2 报错:Type '(x: string) => string' is not assignable to type 'C'.
Types of parameters 'x' and 'x' are incompatible.
Type 'T' is not assignable to type 'string'.(
你唯一能做的就是类似这样写代码,即让 TS 推导。
const identity5:C = (x) => {
return x
}
identity5('5')
全家福
type A<T> = (x:T) => T;
type B<T = string> = (x:T) => T;
type C = <T>(x:T) => T;
function identity(x: string):string {
return x
}
const identity2:A<string> = (x) => {
return x
}
const identity3:B = (x) => {
return x
}
const identity4:B<string> = (x) => {
return x
}
const identity5:C = (x) => {
return x
}
from fe-interview.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
from fe-interview.
Related Issues (20)
- 【每日一题】- 2020-07-28 - Promise.all VS Promise. allSettled Promise.any VS Promise.race HOT 4
- 【每日一题】- 2020-08-03 - 多线程打印 HOT 5
- 【每日一题】- 2020-08-04 - 浏览器是如何解析 CSS rule 的? HOT 3
- 【每日一题】- 2020-08-05 - 运营商劫持 HOT 2
- 【每日一题】- 2020-08-06 - 讨厌的 Y HOT 3
- 【每日一题】- 2020-08-07 - 寻找祖先 HOT 5
- 【每日一题】- 2020-08-10 - JS 编程题 HOT 4
- 【节流防抖的概念弄混了】
- 【每日一题】- 2020-08-21 HOT 3
- 【每日一题】- 2020-08-25 - 编程范式 HOT 1
- 【每日一题】- 2020-09-04 - 多个进程如何监听同一个端口 HOT 2
- 【每日一题】- 2020-09-15 - 响应头 content-type 的奇幻之旅 HOT 2
- 【每日一题】- 2020-10-09 - 以下 shell 的作用是什么? HOT 3
- 【每日一题】- 2020-10-16 - 分割数组 HOT 1
- 【每日一题】- 2020-10-21 - 字典分割 HOT 2
- 【每日一题】- 2020-04-19 - !!~A.indexOf(edge._label) 是什么意思? HOT 3
- 【每日一题】- 2021-07-12 数组的索引为什么从 0 开始?而不是从 1 开始? HOT 3
- 【开源自荐】推荐一个每日更新的前端面试题库
- 【开源自荐】SolidUI 一句话生成任何图形
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fe-interview.