Last active
September 8, 2023 03:12
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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