Skip to content

Instantly share code, notes, and snippets.

@edezekiel
Last active September 8, 2023 03:12
Show Gist options
  • Save edezekiel/8681226a47635d0ab940a0bbfc25ca72 to your computer and use it in GitHub Desktop.
Save edezekiel/8681226a47635d0ab940a0bbfc25ca72 to your computer and use it in GitHub Desktop.
Balance of type safety and flexibility using Record<string, unknown> with spread operator
function parseObject(obj: Record<string, unknown>) {
let doesNotExist = obj['doesNotExist'];
doesNotExist.toString(); // Error: 'doesNotExist' is of type 'unknown'.ts(18046)
doesNotExist += 100; // Error: 'doesNotExist' is of type 'unknown'.ts(18046)
}
parseObject({ ...hero }); // Works
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment