Skip to content

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

Closed
ORESoftware opened this issue Mar 22, 2018 · 8 comments
Closed

Casting to Partial<x> seems to be errorful #22806

ORESoftware opened this issue Mar 22, 2018 · 8 comments
Assignees
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@ORESoftware
Copy link

ORESoftware commented Mar 22, 2018

I have this:

export interface HavenData {
  timeoutAmount: number;
  throwSync?: boolean;
  timeoutThrow: boolean;
  promiseThrow: boolean;
}

 const qs = {timeout: Math.ceil(30 * Math.random())} as Partial<HavenData>;

shouldn't that be an error since timeout is not a member of the HavenData interface? I am not seeing a problem in my IDE.

@DanielRosenwasser
Copy link
Member

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
Copy link
Member

I feel like @RyanCavanaugh might have opinions about this though.

@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Mar 22, 2018
@ORESoftware
Copy link
Author

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>;

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Mar 22, 2018

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.

@jcalz
Copy link
Contributor

jcalz commented Mar 23, 2018

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>'.

@RyanCavanaugh
Copy link
Member

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?

@DanielRosenwasser What is the proposal here?

@RyanCavanaugh RyanCavanaugh added Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. and removed In Discussion Not yet reached consensus labels Apr 16, 2018
@RyanCavanaugh
Copy link
Member

@DanielRosenwasser ping on the above question

@RyanCavanaugh RyanCavanaugh added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript labels Aug 6, 2018
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants