Skip to content

Instantly share code, notes, and snippets.

@Neo42
Last active January 8, 2022 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Neo42/875c466b1e3d867a6ec6b17794c4876e to your computer and use it in GitHub Desktop.
Save Neo42/875c466b1e3d867a6ec6b17794c4876e to your computer and use it in GitHub Desktop.
Mapped types are the for...in loop for types.
interface Fruit {
readonly name?: string
readonly color?: string
readonly sweetness?: number
}
type Properties<Type> = keyof Type
type Values<Type> = Type[Properties<Type>]
type FruitProperties = Properties<Fruit>
type FruitValues = Values<Fruit>
type ReadonlyObject<Type> = {
-readonly [property in Properties<Type>]-?: Type[property]
}
type ReadonlyFruit = ReadonlyObject<Fruit>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment