5 Things They’ll NEVER Add To TypeScript

preview_player
Показать описание

00:00 Intro
00:41 Stricter Omit
02:45 Negated Types
04:28 Nominal/Branded Types
06:41 Throws and Exact Types

Follow Matt on Twitter

Join the Discord:

Рекомендации по теме
Комментарии
Автор

I followed the whole throws conversation on github and I'm pretty disappointed with the arguments for not implementing it:

1) Java has it and people use it wrong – Java doesn't have inference, leading to many functions mindlessly having `throws Exception` by default. That wouldn't happen in TS.
2) You can always get exceptions which are not declared – Irrelevant. Those are always a possibility. Having domain errors in the API would be useful even with no guarantees.
3) Nobody uses the @throws in jsdoc – No popular tool make use of it when you use it. If using it provides no value, people won't use it. That's a flawed metric.
4) You can always return domain errors as values – Not many libraries do it and those that do are not very ergonomic. Go does it, and error handling is all over the code; there's no concept of "happy path". Rust does it great, but it relies on a Result type supported at the language level, with the ? short-circuiting a function like a throw does in JS. It also relies on the Result type itself and the ability to pattern-match against it. JS has nothing close to that.

JS throws. Node, deno, bun, browsers, popular libraries and frameworks… they all throw. Not having a way to encode that is such a weird omission!

(forgive me if I'm misremembering something)

feldinho
Автор

Something indicating that a function can throw an error would be nice, so you'd know to catch it

grzegorz_derdak
Автор

I've proposed the interval types. The reason why I think negated types aren't a thing is because that would make some cases of resolving types extremely hard. Consider the cases where you'd union or intersect a negated type. It can result in a large type pretty quickly.

_nikeee
Автор

I wish they would just allow for `Omit<string, 'click'>` and that's it. That reads intuitively: it can be any string but not 'click'.

mahadevovnl
Автор

5:55 is such a great pattern for when you need extra protection around a set of exported functions. Form validation & sanitization comes to mind.

All of the external functions accept only the branded type and a provided gatekeeper/sanitize function returns the input in the correct branded type.

Bluesourboy
Автор

Instead of StrictOmit, we could use Pick with the props we would like to leave, Pick is typesafe (type Pick<T, K extends keyof T>)

rumble_bird
Автор

The Opaque type is something I wish was easier to do in C++. The relative vs absolute path is a great example, but I've also wanted things like "both position and velocity are vectors, but their arithmetic is constrained". Very educational to see how this problem is addressed in Typescript, thank you!

avramlevitter
Автор

Brand types can be applied without casting, if brand property is optional

munzamt
Автор

Nominal types are also referred to as "newtypes" in other languages. The library "gcanti/newtype-ts" provides a cool implementation that you can use :D

neolight
Автор

You forgot the most important one, why they do not type Object.keys for narrowed types and just make them string. 😉

rickvandenbroek
Автор

Typescript does have nominal types for classes with private members, you can also use those for branding

ex-xghh
Автор

I think I can hear the rain at the end of the video, anyway thanks for the awesome vid Matt !

hut_fire
Автор

I love the structural type system of TS, but I've hoping for nominal types since the day I found out about them

markzuckerbread
Автор

2:44 type Spread<T1, T2> = T2 & BetterOmit<T1, keyof T2 & keyof T1>;

nomadshiba
Автор

I thought you'd at least list some of the things you fixed with ts-reset :P

TheAlexLichter
Автор

Not only do I have to program JS in TS every day, but now I also have to program the Typings for TS. Stop the torture :(

SergiySev
Автор

matt hi, this one of best channels about advanced ts, great content. Can you make a video about project references? how to use them? implications? how to work with them when we are using bundlers ? pros vs contras. I'm moving one large monorepo to ts project references, but i found i have to make a build each time i wanna typecheck code and that is generating slow performance, even if its just declaration files, but is taking so much

robertomolinasilvera
Автор

IMPORTANT FOR MATT

Hi Matt. You, as a TS ninja, whose opinion on TS is important worldwide, can affect TS team on the following issue :) Or maybe you can explain why stuff that I will describe below is as it is:

For example I declared type:

type MyType = {
name: string
values: number[]
}

const myObject: MyType = {
name: "Batman"
values: [3, 5, 8]
}of

Why TS allows to doubt here:
myObject.values?.filter(num => num > 10)

Why it allows to use this question mark? We definitely know that values is mandatory, and they are represented by array. It should say something like - hey man, this question mark is redundant.

Please, help to understand why it works like this

Kind regards

kirillzlobin
Автор

`Exclude<string, 'click'>` should work no ?

LePetitMondedePurexoYT
Автор

The TypeScript God is trying to tell you something Matt

detaaditya