danvk / effective-typescript

Effective TypeScript 2nd Edition: 83 Specific Ways to Improve Your TypeScript
https://effectivetypescript.com
Other
1.56k stars 226 forks source link

Use typed identity functions to guide type inference #25

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

Use typed identity functions to guide type inference

Effective TypeScript: Use typed identity functions to guide type inference

https://effectivetypescript.com/2020/06/16/typed-identity-functions/

danvk commented 1 year ago

While the tuple helper still has its place, TypeScript 4.9's satisfies operator has made the last two examples obsolete.

Instead of withValueType<T>, you can use satisfies Record<string, T>:

const capitals = {
  ny: [-73.7562, 42.6526],
  ca: [-121.4944, 38.5816],
  ak: [-134.4197, 58.3019],
} satisfies Record<string, Point>;

~Instead of withValueTypesFrom, you can use satisfies Partial<T>:~ This doesn't quite do the same thing!

Progress!