Skip to content

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

Closed

Description

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.

Activity

  1. DanielRosenwasser commented on Mar 22, 2018

    @DanielRosenwasser
    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.

  2. DanielRosenwasser commented on Mar 22, 2018

    @DanielRosenwasser
    Member

    I feel like Ryan Cavanaugh (@RyanCavanaugh) might have opinions about this though.

  3. ORESoftware commented on Mar 22, 2018

    @ORESoftware
    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>;
  4. DanielRosenwasser commented on Mar 22, 2018

    @DanielRosenwasser
    Member

    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.

  5. jcalz commented on Mar 23, 2018

    @jcalz
    Contributor

    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>'.
  6. RyanCavanaugh commented on Mar 23, 2018

    @RyanCavanaugh
    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?

    Daniel Rosenwasser (@DanielRosenwasser) What is the proposal here?

  7. added
    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.
    and removed on Apr 16, 2018
  8. RyanCavanaugh commented on Apr 16, 2018

    @RyanCavanaugh
    Member

    Daniel Rosenwasser (@DanielRosenwasser) ping on the above question

  9. added
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    and removed
    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.
    SuggestionAn idea for TypeScript
    on Aug 6, 2018
  10. typescript-bot commented on Dec 15, 2018

    @typescript-bot
    Contributor

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

  11. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Working as IntendedThe behavior described is the intended behavior; this is not a bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions