Skip to content
0:00 / 0:00

#variance#covariance#contravariance

TS Meetup #5.5 - Tițian Cericova-Dragomir - Understanding Variance in the TypeScript Type System

Prisma

28K subscribers • 2 videos on Hallway

YouTube
  • Recommended by 2 people: 1 in engineering communities and 1 on Stack Overflow and its sister sites.
  • Presented by Tițian Cericova-Dragomir, engineer at Bloomberg

Advanced. Deep dive for people who know the field.

3.3K views5 years ago2 recommendations

Programming languagesComputer scienceTypeScriptComputation and complexity

Tițian Cericova-Dragomir explains covariance, contravariance, bivariance, and invariance within the TypeScript type system.

◭ Ever wondered why Array is assignable to Array, even though that doesn’t really seem safe? Have you seen the term ‘variance’ thrown around by TypeScript people, but haven’t gotten around to researching what that is? Then this is the talk for you! We will take a deep dive into the type system, understand the other way TypeScript determines assignability. We will start with the basics of what variance is, and then we will see how it impacts every aspect of the type system. ◭ Tițian Cericova-Dragomir (@TitianCernicova) is an engineer at Bloomberg, working on TypeScript and JavaScript infrastructure. He is a TypeScript compiler contributor, as well as one of the top answerers with regard to TypeScript on Stack Overflow. Also, he is a huge fan of TypeScript, and he will try to convince you that you should be one too. ✨ Join our Prisma Meetup group here: meetup.com/TypeScript-Berlin/ ------------------------ Next: 👉 Check Next video: PropTypes vs TypeScript - youtu.be/kOB0PcVEuZg 👉 Check Previous video: Elm in TypeScript, pattern matching and beyond - youtu.be/OXzIZZDq2hE ------------------------ 📚 Resources: ✅ Subscribe to Prisma: youtube.com/channel/UCptAHlN1gdwD89tFM3ENb6w?sub_confirmatio… ✅ Learn more about Prisma: ◭ Website: prisma.io ◭ Docs: prisma.io/docs ◭ Quickstart: pris.ly/qstart ------------------------ 💬 Connect with Prisma: Twitter: twitter.com/prisma Discord: pris.ly/discord LinkedIn: linkedin.com/company/prisma-io/

Why it's here

2 people recommend this

What people say. Highly recommended by engineers as an excellent, deep-dive explanation of variance in TypeScript.

u/mirpetriin r/typescript • Recommends

On “Trying to make a function which returns an intersection type bsaed on the arguments”

I had no idea there is this co/contra/bi/in-variance thing going on in typescript: link to this video

Alexander Nenashevon Stack Overflow • Recommends

On “TypeScript allows passing Readonly<T> to a mutating function”

Parameter variance in TS is a kind of mess. A good video about variance: link to this video. Seems in the case the parameter is bivariant. I would try to incorporate "mutable" branded type, that way you could force using Readonly (it's convoluted but makes some sense): Playground { type F<T> = (t: Readonly<T>) => void; type Params = Mutable<{ a: number }>; type Mutable<T> = T & {__mutable: never}; type Readonly<T> = Omit<{ readonly P in keyof T: TP; }, '__mutable'>; // a utility to easily create mutable values; const mutable = <T,>(a: T) => a as Mutable<T>; const params = {a: 1}; mutate(params); // ERROR mutate2(params); // OK const params2 = mutable({a: 1}); mutate(params2)…