79111148

Date: 2024-10-21 17:38:52
Score: 1
Natty:
Report link

Typescript 4.9 (released Nov 15, 2022) makes this easy with the satisfies keyword.

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-9.html#the-satisfies-operator

In summary, it validates the types, but still keeps the specific type for inference.


type PageDef = {url: string}

const pages = {
    home:  { url: '/' },      // works!
    about: { url: '/about' }, // works!
    bad:   { asdf: true }     // error!
} satisfies Record<string, PageDef>

pages.home          // works!
pages.about         // works!
pages.nonexistent   // error!

Link to TS Playground

Reasons:
  • Probably link only (1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Pranav Nutalapati