Skip to content
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

TypeScript unexpectedly inferring 'any' type. #30250

Closed
CyrusNajmabadi opened this issue Mar 7, 2019 · 11 comments
Closed

TypeScript unexpectedly inferring 'any' type. #30250

CyrusNajmabadi opened this issue Mar 7, 2019 · 11 comments
Labels
Duplicate An existing issue was already created

Comments

@CyrusNajmabadi
Copy link
Contributor

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!

@CyrusNajmabadi
Copy link
Contributor Author

Tagging @DanielRosenwasser

@CyrusNajmabadi
Copy link
Contributor Author

@RyanCavanaugh do you have any ideas here? Thanks!

@RyanCavanaugh
Copy link
Member

This problem was fixed by #29740

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 7, 2019
@CyrusNajmabadi
Copy link
Contributor Author

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

@RyanCavanaugh
Copy link
Member

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

@CyrusNajmabadi
Copy link
Contributor Author

@RyanCavanaugh wasn't it almost a month ago?

image

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

@RyanCavanaugh
Copy link
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

@CyrusNajmabadi
Copy link
Contributor Author

I thought today was February 7th 🤦‍♂️

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

@CyrusNajmabadi
Copy link
Contributor Author

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

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

@typescript-bot
Copy link
Collaborator

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

@RyanCavanaugh
Copy link
Member

See #30281

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants