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
Type assertions are meant to be as lenient as possible, so excess property errors won't occur; that means you're almost always able to use a type assertion with something that's all-optional.
Nope, the two are semantically equivalent; x as Foo was introduced because of parsing problems related to JSX, but it works the same in the type system.
If you want the compiler to complain, I'd go for a type annotation instead of an assertion:
constqs: Partial<HavenData>={timeout: Math.ceil(30*Math.random())};// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// Type '{ timeout: number; }' is not assignable to type 'Partial<HavenData>'.// Object literal may only specify known properties, and 'timeout' does not exist // in type 'Partial<HavenData>'.
Reacted by Alexander Mills, Sean M. Vieira, Griffork, Zhongliang Wang and Marin Marinov
Activity
DanielRosenwasser commentedon Mar 22, 2018
Type assertions are meant to be as lenient as possible, so excess property errors won't occur; that means you're almost always able to use a type assertion with something that's all-optional.
DanielRosenwasser commentedon Mar 22, 2018
I feel like Ryan Cavanaugh (@RyanCavanaugh) might have opinions about this though.
ORESoftware commentedon Mar 22, 2018
I was thinking that this was like "F you, just do what I want":
but that this was more like "please complain if the cast will likely not work":
DanielRosenwasser commentedon Mar 22, 2018
Nope, the two are semantically equivalent;
x as Foowas introduced because of parsing problems related to JSX, but it works the same in the type system.jcalz commentedon Mar 23, 2018
If you want the compiler to complain, I'd go for a type annotation instead of an assertion:
RyanCavanaugh commentedon Mar 23, 2018
I'm vaguely surprised this isn't an error due to
Partial<T>being a weak type. Did we carve out something specific that makes this work?Daniel Rosenwasser (@DanielRosenwasser) What is the proposal here?
RyanCavanaugh commentedon Apr 16, 2018
Daniel Rosenwasser (@DanielRosenwasser) ping on the above question
typescript-bot commentedon Dec 15, 2018
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.