-
-
Notifications
You must be signed in to change notification settings - Fork 532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PickDeep
strips null
#880
Comments
Thanks report, but I don't think this is a bug Let's look some cases: case 1type Pet = {
owner: { name: 'name1' }
| { name: 'name2' ; foo: '' };
};
type Result = PickDeep<Pet, 'owner.name'>;
type Result = {
owner: { name: 'name1' } | { name: 'name2' }
} case 2type Pet = {
owner: { name: 'name1' }
| { foo: '' };
};
type Result = PickDeep<Pet, 'owner.name'>;
type Result = {
owner: { name: 'name1' }
} case 3type Pet = {
owner: { name: 'name1' }
| null;
};
type Result = PickDeep<Pet, 'owner.name'>; so according to case 2, type Result = {
owner: { name: 'name1' }
} This is the same as the current behavior |
That makes sense as to why it behaves like this but to me it seems like unexpected or maybe less ergonomic behavior. My current use case is narrowing types on function inputs like so type Owner = {
name: string;
age: number;
}
type Pet = {
name: string;
age: number;
owner: Owner | null;
}
// I only use `name` and `owner.name`
function printGreeting(pet: PickDeep<Pet, "name" | "owner.name"> {
if (pet.owner) {
console.log(`Hi, my name is ${pet.name} and my owner is ${pet.owner.name}!`);
} else {
console.log(`Hi, my name is ${pet.name} and I'm looking for an owner!`);
}
} Right now the function would require |
Okay, you convinced me. maybe we should keep union in What do you think? @sindresorhus |
What would "case 2" become if so? |
@sindresorhus I actually thought about this a bit when writing my response. This behavior is not consistent with how type Pet = {
name: string;
owner: { name: 'name1' } | { foo: '' };
};
declare const pet: Pet;
function onlyNeedsOwnerName(pet: PickDeep<Pet, "owner.name">) {
...
}
onlyNeedsOwnerName(pet)
// ^ will result in an error Again, in this case |
that will be type Result = {
owner: { name: 'name1' } | { foo: "" }
} It's look like confused, I am beginning to waver on whether need do this for "type safety" |
If you have following types
the resulting type of
PickDeep<Pet, "name" | "owner.name">
isUpvote & Fund
The text was updated successfully, but these errors were encountered: