-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Property of a type is not assignable to Pick of this type #57138
Comments
The type of
|
Right. Probably I oversimplified my example. I will see if I can reproduce it with more complex types. |
I made more complex example that shows problem I encountered in real code. Code: type Item = {
title: string
categories?: (string | Category)[] | null
}
type Category = {
id: string
slug: string
}
const allItems:Item[] = []
type ItemNarrowed = Pick<Item, 'title'> & {
categories?: Pick<Category, 'id' | 'slug'>[] // | null
}
function a(item: ItemNarrowed) {console.log(item)}
allItems
.filter(item => typeof item.categories !== 'undefined' && item.categories !== null && !!item.categories) // only items where categories is not null and not undefined
// function call gives error: Type 'null' is not assignable to type 'Pick<Category, "id" | "slug">[] | undefined'.
.filter(item => Array.isArray(item.categories)) // only items where categories is array (same as previous filter)
// gives same error
.filter(item => typeof item.categories !== 'string') // only items whre item.categories is Category
// if you uncomment null in ItemNarrowed.categories, it will give this error instead: Type 'string' is not assignable to type 'Pick<Category, "id" | "slug">'.
.forEach(item => {
a(item)
}) So unless I did something wrong, .filter() does nothing for type narrowing. |
That's correct: type predicates are not automatically inferred. You need to write e.g. |
Yes, it appears to be the only actual problem. |
All our progress, commits, and meeting notes are public. For approximately all things, if you don't see it here, it's not being worked on due to the other work you see having higher priority. |
This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
🔎 Search Terms
"TS2322 pick"
"not assignable to pick"
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play?ts=5.4.0-dev.20240123#code/C4TwDgpgBAksEFsoF4oG8CwAoKurAEtgAbCAfgC4oBnYAJwIDsBzKAHykYFdjjsBfbNgDGAe0a0oRRFTiIU6bHnxFSVAOQALCL1HqBQrADMujYYXFQAhgApCJCFQAKBYQGsAPHIQAaKOvtSdQA+AEp0MQlRUgA6YlFmO1UIUMFjU3MCSwAjJIdnV09vPwDkkPYoUwATCCMmCCrwtEjqaIg4hLzSVOwTMwtGKGEuxygXdy94X39AiHKOatr6qoruXiaWto7E2Z6sbFtpBBjd7Fyjk+TQkRsL3aA
💻 Code
🙁 Actual behavior
Type
Item
has propertytitle
which isstring | null | undefined
.Trying to assign property
title
of typeItem
to consumer with typePick<Item, 'title'>
results in error ts2322 - cannot assignundefined
to property which already supportsundefined
:But what if we explicitly add
undefined
to consumer?Pick<Item, 'title'> | undefined
results in same error ts2322 - but now it is aboutnull
, which consumer already supports:What if we explicitly add
null
to consumer too?Pick<Item, 'title'> | undefined | null
results in same error ts2322 - but now that we manually redefined two thirds of initial type, it fails to assignstring
to the only remaining option from picked type, which isstring
:🙂 Expected behavior
All 3 options (pick, pick undefined, pick undefined null) should work without error because all possible types of consumer are derived (and/or overwritten by identical types) from the same type definition that argument in function call uses - thus both have exactly same type.
Additional information about the issue
I searched for other occurrences of ts2322 error, but non of them were in context of Pick, and workarounds for them were not applicable here.
The text was updated successfully, but these errors were encountered: