Skip to content

TypeScript unexpectedly inferring 'any' type. #30250

Closed

Description

Given the following code:

interface Widget { x: number }

declare type Unwrap<T> = T extends Promise<infer U1> ? UnwrapSimple<U1> : UnwrapSimple<T>;
declare type primitive = Function | string | number | boolean | undefined | null;

declare type UnwrapSimple<T> = T extends primitive ? T : T extends Array<infer U> ? UnwrappedArray<U> : T extends object ? UnwrappedObject<T> : never;

export interface UnwrappedArray<T> extends Array<Unwrap<T>> {
}

export declare type UnwrappedObject<T> = {
    [P in keyof T]: Unwrap<T[P]>;
};

declare var w: Widget;
declare type TupleType = [Widget, string];
declare var o: UnwrappedObject<TupleType>;

TypeScript oddly infers a type of [any, string] for o. This is surprising to us given how we would think these type operators would work. First, given the definition UnwrappedObject i would expect UnwrappedObject<TupleType> to expand like so:

UnwrappedObject<TupleType>  ->
UnwrappedObject<[Widget, string]> ->
[Unwrap<Widget>, Unwrap<string>] ->
[UnwrappedObject<Widget>, string] ->
[{ x: Unwrap<number> }, string] ->
[{ x: number }, string]

HOwever, instead of getting { x: number } (which is just the Widget type), we end up with 'any'. Cany anyone shed any light on this? Thanks!

Activity

  1. CyrusNajmabadi commented on Mar 7, 2019

    @CyrusNajmabadi
    ContributorAuthor
  2. CyrusNajmabadi commented on Mar 7, 2019

    @CyrusNajmabadi
    ContributorAuthor

    Ryan Cavanaugh (@RyanCavanaugh) do you have any ideas here? Thanks!

  3. RyanCavanaugh commented on Mar 7, 2019

    @RyanCavanaugh
    Member

    This problem was fixed by #29740

  4. CyrusNajmabadi commented on Mar 7, 2019

    @CyrusNajmabadi
    ContributorAuthor

    Ryan Cavanaugh (@RyanCavanaugh) Thanks! Is there a way to tell what release of TS that may have made it out into?

  5. RyanCavanaugh commented on Mar 7, 2019

    @RyanCavanaugh
    Member

    The PR was 3 days ago, so it'd be in any nightly starting approximately yesterday

  6. CyrusNajmabadi commented on Mar 7, 2019

    @CyrusNajmabadi
    ContributorAuthor

    Ryan Cavanaugh (@RyanCavanaugh) wasn't it almost a month ago?

    image

    This is part of github that i'm not really familiar with!

  7. RyanCavanaugh commented on Mar 7, 2019

    @RyanCavanaugh
    Member

    I thought today was February 7th 🤦‍♂️

    This fix wasn't in 3.3, so "nightly" is still the right answer.

    The incantation (if you have a local TS repo) to check is

    D:\github\TypeScript>git branch -r --contains d9ee867
      origin/HEAD -> origin/master
      origin/master
      origin/no-mkdir-race
      upstream/add-globalThis
      upstream/applyChangesToOpenFiles
      upstream/circularConstraintErrors
      upstream/contextualGenericRestParameter
      upstream/convert-to-named-parameters
      upstream/incrementalBuildInfo
      upstream/master
      upstream/perfTimeStamp
      upstream/revertExecFileSync
      upstream/usePrependToSkipBuild
      upstream/weswigham-patch-1
  8. CyrusNajmabadi commented on Mar 7, 2019

    @CyrusNajmabadi
    ContributorAuthor

    I thought today was February 7th 🤦‍♂️

    That's ok. I still think it's 2017 occasionally. :D

  9. CyrusNajmabadi commented on Mar 8, 2019

    @CyrusNajmabadi
    ContributorAuthor

    Thanks. Have confirmed this is fixed in the latest nightly. I'm going to keep this issue open though until that is released.

    Ryan Cavanaugh (@RyanCavanaugh) Daniel Rosenwasser (@DanielRosenwasser) Any general idea of when 3.4 might be released? Trying to make plans depending on if it's more like in a week or so, or more like a month. Thanks much!

  10. typescript-bot commented on Mar 11, 2019

    @typescript-bot
    Contributor

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

  11. RyanCavanaugh commented on Mar 11, 2019

    @RyanCavanaugh
    Member

    See #30281

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

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions