-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
(Partial<Type>)[keyof Type] is udefined in certain settings and assignment is not possible #45003
Comments
Related: #32693 |
This is the correct behavior. metadata[key as keyof Metadata] = props[key as keyof CreateProps]!; The same logic applies here. |
See also #30769. In particular:
Emphasis mine. |
Thanks. Is there any workaround to allow the assignment besides using ts-ignore? |
I would a type assertion corresponding to why the code isn't actually wrong, e.g. |
I'm sorry, to be more precise I'm looking for a workaround to the more complex example. I understand that the compiler is right, but I'm not able to code something which is pretty standard without using ts-ignore on the error line. type TableData = {
id: string
title: string
icon: string
}
type Metadata = {
url: string
active: boolean
}
type CreateProps = Omit<TableData, 'id'> & Partial<Metadata>;
function create(props: CreateProps): void {
const id = 'test';
const tableData: TableData = {
id,
title: props.title,
icon: props.icon
}
const metadata: Partial<Metadata> = { };
for (const key of Object.keys(props)) {
// Build metadata from those props which are not tableData
if (typeof tableData[key as keyof TableData] === 'undefined') {
// Error:
// Type 'string | boolean' is not assignable to type 'undefined'
metadata[key as keyof Metadata] = props[key as keyof CreateProps]!;
}
}
} |
After some more thinking, using ts-ignore is probably the most elegant option if the actual solution would require writing some additional generic types. Unless there is something simple. |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Just coming back to it after some time, instead of using @ts-ignore, I would do this today: metadata[key as keyof Metadata] = props[key as keyof CreateProps] as unknown as undefined; Not pretty, but come to think of it the key type is any arbitrary key and there's no relation between them so casting has to be used anyway, there's probably a more correct way to do it but it would be way too much effort for an internal implementation of one function. |
Bug Report
🔎 Search Terms
Partial keyof undefined
🕗 Version & Regression Information
I'm using 4.2.4 in production and it occurs. I have tested with 4.4 beta on the playground and it also occurs. I have no knowledge if the bug occurred in prior versions.
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
data2[key as keyof Data2]
isundefined
and it's not possible to assign anything to it.🙂 Expected behavior
data2[key as keyof Data2]
should not beundefined
. At least it should beundefined | string | boolean
. It should also be possible to assign a value to it, despite being possibleundefined
.The text was updated successfully, but these errors were encountered: