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
Currently type assertions do not seem to respect typed index signatures [key: string]: any; in interfaces or test for compatibility before attempting to convert.
I am proposing that type assertions would be able to convert this situation without error:
Currently to work around this I need to first create an intermediate variable that is typed as a Partial before making the assertion. I would prefer to just make the assertion.
Examples
This example above will error because {b: 'b', c: 3 } is missing property a
This could potentially open up a lot of problematic comparisons just because those types have index signatures.
It's wasn't immediately clear to me (i.e. at a glance) which direction of the comparability relationship this becomes more lenient for. It appears that the change would be Test ≤ { b: string, c: number } where ≤ refers to comparability).
Just revisiting this to clarify:
Given that we have a value V when asserting a type T for V then V should also be tested to see if it can be assigned to a Partial of T
(As seen in the workaround in the description)
Another possibly viable solution would be to just make an assertion on an object implicitly assert a partial of the type originally asserted.
Search Terms
interface, index, signature, typed, assertion, conversion, partial,
Suggestion
Currently type assertions do not seem to respect typed index signatures
[key: string]: any;
in interfaces or test for compatibility before attempting to convert.I am proposing that type assertions would be able to convert this situation without error:
Use Cases
Currently to work around this I need to first create an intermediate variable that is typed as a Partial before making the assertion. I would prefer to just make the assertion.
Examples
This example above will error because
{b: 'b', c: 3 }
is missing propertya
playground
If I remove
c: 3
from the object then there is no error anda
is no longer required:playground
Currently to workaround this an intermediate typed value can be used so the subtype is already confirmed before the assertion:
playground
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: