You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constkTableColumns=['id','title','icon'];typeTableData={id: stringtitle: stringicon: string}typeMetadata={url: string}typeUpdateProps=Partial<Omit<TableData,'id'>>&Partial<Metadata>;functionupdate(props: UpdateProps){consttableUpdate: Array<{column: string,value: string}>=[];kTableColumns.forEach((column)=>{if(typeofprops[columnaskeyofUpdateProps]!=='undefined'){// Explicitly checked that props[columns] is not undefined, yet I have to// use non-null assertion operator to satisfy the compiler.// Error: string | undefined is not assignable to type stringtableUpdate.push({ column,value: props[columnaskeyofUpdateProps]})// Only ! can fix it.// Besdies this, the `as keyof UpdateProps` should be redundant after// checking that the value is not undefined.}});}
🙁 Actual behavior
After validating that prop[columns] is not undefined, the compiler still treats prop[columns]as possiblyundefined`.
A non-null assertion operator at the end of the value is a workaround, but it feels like it should not be there.
🙂 Expected behavior
The type of prop[columns] inside the if clause should not be undefined.
Additionally, using column as keyof UpdateProps should not be required after validating that column is a valid key of the object (though I understand if there are valid reasons against it, I'm mostly concerned about the first issue).
The text was updated successfully, but these errors were encountered:
Bug Report
🔎 Search Terms
type guard object property
🕗 Version & Regression Information
I'm using 4.2.4 in production, I don't know if this bug has occurred in prior versions.
I have tested in the playground that the bug also occurs in 4.3.x and 4.4 beta
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
After validating that
prop[columns] is not
undefined, the compiler still treats
prop[columns]as possibly
undefined`.A non-null assertion operator at the end of the value is a workaround, but it feels like it should not be there.
🙂 Expected behavior
The type of
prop[columns]
inside the if clause should not be undefined.Additionally, using
column as keyof UpdateProps
should not be required after validating that column is a valid key of the object (though I understand if there are valid reasons against it, I'm mostly concerned about the first issue).The text was updated successfully, but these errors were encountered: