Get: Distribute over unions so a key missing on some members yields undefined#1463
Get: Distribute over unions so a key missing on some members yields undefined#1463ardittirana wants to merge 2 commits into
Get: Distribute over unions so a key missing on some members yields undefined#1463Conversation
… `undefined`
For a union base type, `Get` accessed the key on each member but a member
lacking the key resolved to `unknown`, which absorbed the whole union (e.g.
`Get<{a: number} | {b: string}, 'a'>` was `unknown` instead of `number | undefined`).
`PropertyOf` now checks for a union first and, when distributing, treats a
missing key as `undefined` for that member, while a missing key on a non-union
type still resolves to `unknown` (unchanged, documented behaviour).
Fixes sindresorhus#1205.
som-sm
left a comment
There was a problem hiding this comment.
A missing key on a non-union type still resolves to
unknown(the existing, documented behaviour — preserved by an explicitMissingPropertyparameter).
I think the existing behaviour here is incorrect, this should also be undefined. More so, if for unions it's going to be undefined. Let's make it consistently undefined for missing properties.
Following review: a key that is not present on the base type now resolves to `undefined` rather than `unknown`, for both union and non-union base types. This matches the behaviour of deep-key libraries like lodash, and makes the result consistent with the union-distribution case (where a key missing on some members already became `undefined`). The `MissingProperty` parameter on `SinglePropertyOf` is no longer needed and is removed. Tests for out-of-bounds and missing-key access are updated from `unknown` to `undefined`.
|
Makes sense — consistency wins here, and you're right that it's odd to special-case non-unions once unions resolve missing keys to Changes:
Full |
Fixes #1205
What was wrong
For a union base type,
Getaccessed the key on each member, but a member that lacked the key resolved tounknown— andunknownabsorbs the rest of the union. So:Fix
PropertyOfnow detects a union first and, when distributing over its members, treats a missing key asundefinedfor that member. A missing key on a non-union type still resolves tounknown(the existing, documented behaviour — preserved by an explicitMissingPropertyparameter).Tests
Added cases to
test-d/get.tsfor: key present on all members, key present on some members (→number | string | undefined), a direct union base type, and the unchanged non-unionunknowncase.npm run test:tsdandxopass; no existing assertions changed.