danvk / effective-typescript

Effective TypeScript 2nd Edition: 83 Specific Ways to Improve Your TypeScript

Home Page:https://effectivetypescript.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use typed identity functions to guide type inference

utterances-bot opened this issue · comments

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/

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!