Giter VIP home page Giter VIP logo

Comments (13)

Arichy avatar Arichy commented on June 16, 2024 2

有一个问题,在《深入理解typescript》这本书说到函数“返回值类型是协变的,而参数类型是逆变的”,怎么理解“返回值类型是协变的”?

举个例子

let returnAnimal = ():Animal => {
  return animal;
}

let returnDog = ():Dog => {
  return dog;
}

因为 AnimalDog 的父类型,并且 returnAnimal = returnDog 合法,所以返回值类型是协变的,即:
Animal extends Dog
():Animal extends ():Dog

至于为什么合法,因为 returnDog 获得了一个 Dog,而 returnAnimal 预期是获得一个 Animal,在获得后只会访问 Animal 的属性/方法,不会产生类型错误。

其实总结一下返回值类型协变+参数类型逆变,两者不同是因为考虑安全性的 block 不同

  • 返回值类型需要在调用函数的 block 里考虑,此时返回值相当于类型自身。在一个 block 里,一个函数 return 某个类型,和我直接放一个某个类型的变量在那里,两者是完全等价的
  • 参数类型需要在等号后面那个函数的 block 里考虑。(Parent)=>void = (Child)=>void 本质上是在 (Child)=>void 这个函数的 block 里,执行了一次 Child = Parent 的参数赋值操作,显然是不安全的,所以需要反过来

from blogs.

jacinyan avatar jacinyan commented on June 16, 2024

棒👏🏻

from blogs.

penghei avatar penghei commented on June 16, 2024

有一个问题,在《深入理解typescript》这本书说到函数“返回值类型是协变的,而参数类型是逆变的”,怎么理解“返回值类型是协变的”?

from blogs.

akai-cn avatar akai-cn commented on June 16, 2024

非常感谢,看了一堆文章,在你的基础上瞎说了一堆,还好文末附了您的链接,讲的很通透!

from blogs.

610076912 avatar 610076912 commented on June 16, 2024

from blogs.

longshishui avatar longshishui commented on June 16, 2024

from blogs.

IWSR avatar IWSR commented on June 16, 2024

例子直观,终于理解逆变是啥了

from blogs.

longshishui avatar longshishui commented on June 16, 2024

from blogs.

longshishui avatar longshishui commented on June 16, 2024

from blogs.

guda-art avatar guda-art commented on June 16, 2024

描述一个对象 属性越多越具体
描述一个值 范围越窄越具体

from blogs.

610076912 avatar 610076912 commented on June 16, 2024

from blogs.

BryanAdamss avatar BryanAdamss commented on June 16, 2024

有一个问题,在《深入理解typescript》这本书说到函数“返回值类型是协变的,而参数类型是逆变的”,怎么理解“返回值类型是协变的”?

举个例子

let returnAnimal = ():Animal => {
  return animal;
}

let returnDog = ():Dog => {
  return dog;
}

因为 AnimalDog 的父类型,并且 returnAnimal = returnDog 合法,所以返回值类型是协变的,即: Animal extends Dog ():Animal extends ():Dog

至于为什么合法,因为 returnDog 获得了一个 Dog,而 returnAnimal 预期是获得一个 Animal,在获得后只会访问 Animal 的属性/方法,不会产生类型错误。

其实总结一下返回值类型协变+参数类型逆变,两者不同是因为考虑安全性的 block 不同

  • 返回值类型需要在调用函数的 block 里考虑,此时返回值相当于类型自身。在一个 block 里,一个函数 return 某个类型,和我直接放一个某个类型的变量在那里,两者是完全等价的
  • 参数类型需要在等号后面那个函数的 block 里考虑。(Parent)=>void = (Child)=>void 本质上是在 (Child)=>void 这个函数的 block 里,执行了一次 Child = Parent 的参数赋值操作,显然是不安全的,所以需要反过来

总结的很通透👏

函数类型兼容,我就记住了「参逆反协」规则

这两篇文章说的也挺好的
https://dmitripavlutin.com/typescript-covariance-contravariance/#google_vignette
https://jkchao.github.io/typescript-book-chinese/tips/covarianceAndContravariance.html

from blogs.

610076912 avatar 610076912 commented on June 16, 2024

from blogs.

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.