Giter VIP home page Giter VIP logo

Comments (3)

suukii avatar suukii commented on May 26, 2024
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.

azl397985856 avatar azl397985856 commented on May 26, 2024

结论

  • 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.

stale avatar stale commented on May 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.