-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Casting to Partial<x> seems to be errorful #22806
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
Comments
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. |
I feel like @RyanCavanaugh might have opinions about this though. |
I was thinking that this was like "F you, just do what I want": const qs = <Partial<HavenData>>{timeout: Math.ceil(30 * Math.random())}; but that this was more like "please complain if the cast will likely not work": const qs = {timeout: Math.ceil(30 * Math.random())} as Partial<HavenData>; |
Nope, the two are semantically equivalent; |
If you want the compiler to complain, I'd go for a type annotation instead of an assertion: const qs: 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>'. |
I'm vaguely surprised this isn't an error due to @DanielRosenwasser What is the proposal here? |
@DanielRosenwasser ping on the above question |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
I have this:
shouldn't that be an error since
timeout
is not a member of theHavenData
interface? I am not seeing a problem in my IDE.The text was updated successfully, but these errors were encountered: