Last active
March 7, 2019 09:02
-
-
Save gunar/d4f03d7ad9dc891b4a041f39d8bcbf7a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Proof: https://www.typescriptlang.org/play/#src=type%20Woman%20%3D%20%7B%0D%0A%20%20id%3A%20Number%0D%0A%20%20husband%3F%3A%20string%0D%0A%20%20%2F%2F%20Anytime%20you%20need%20to%20add%20a%20new%20field%20here%0D%0A%7D%0D%0A%0D%0A%2F%2F%20You%20have%20to%20update%20this%20vvvvvvvvvvvvvvvvvvvvvvvvvvv%0D%0Aconst%20processMrs%20%3D%20(mrs%3A%20%7B%20id%3A%20Number%2C%20husband%3A%20string%20%7D)%20%3D>%20%7B%0D%0A%20%20%20%20const%20%7B%20husband%20%7D%20%3D%20mrs%0D%0A%20%20%20%20console.log(%60I%20now%20for%20a%20fact%20%24%7Bmrs%7D%20is%20married.%60)%0D%0A%7D%0D%0AprocessMrs(%7B%20id%3A%201%2C%20husband%3A%20'John'%20%7D)%20%20%20%20%2F%2F%20passes%0D%0AprocessMrs(%7B%20id%3A%202%2C%20husband%3A%20undefined%20%7D)%20%2F%2F%20fails%20(with%20strictNullChecks%3A%20true)%0D%0AprocessMrs(%7B%20id%3A%203%20%7D)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20fails%20(with%20strictNullChecks%3A%20true)%0D%0A%0D%0A%2F%2F%20And%20this%20%20%20%20%20%20%20%20%20%20%20%20%20%20vvvvvvvvvv%0D%0Aconst%20processMs%20%3D%20(ms%3A%20%7B%20id%3A%20Number%20%7D)%20%7B%0D%0A%20%20console.log(%60I%20know%20for%20a%20fact%20%24%7Bms%7D%20is%20not%20married.%60)%0D%0A%7D%0D%0AprocessMs(%7B%20id%3A%201%2C%20husband%3A%20'John'%20%7D)%20%20%20%20%2F%2F%20fails%20(with%20strictNullChecks%3A%20true)%0D%0AprocessMs(%7B%20id%3A%202%2C%20husband%3A%20undefined%20%7D)%20%2F%2F%20fails%20(with%20strictNullChecks%3A%20true)%0D%0AprocessMs(%7B%20id%3A%203%20%7D)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20passes%0D%0A%0D%0A%2F%2F%20%40gunar%202018-03-07%0D%0A%2F%2F%20https%3A%2F%2Fmedium.com%2F%40gunar%2Fnarrower-optional-fields-for-typescript-49fd68335601 | |
type Woman = { | |
id: Number | |
husband?: string | |
// Anytime you need to add a new field here | |
} | |
// You have to update this vvvvvvvvvvvvvvvvvvvvvvvvvvv | |
const processMrs = (mrs: { id: Number, husband: string }) => { | |
const { husband } = mrs | |
console.log(`I now for a fact ${mrs} is married.`) | |
} | |
processMrs({ id: 1, husband: 'John' }) // passes | |
processMrs({ id: 2, husband: undefined }) // fails (with strictNullChecks: true) | |
processMrs({ id: 3 }) // fails (with strictNullChecks: true) | |
// And this vvvvvvvvvv | |
const processMs = (ms: { id: Number }) { | |
console.log(`I know for a fact ${ms} is not married.`) | |
} | |
processMs({ id: 1, husband: 'John' }) // fails (with strictNullChecks: true) | |
processMs({ id: 2, husband: undefined }) // fails (with strictNullChecks: true) | |
processMs({ id: 3 }) // passes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is part of Narrower Optional Fields for TypeScript.