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

infer recursive type (Typescript) #23433

Closed
mjbvz opened this issue Apr 16, 2018 · 4 comments
Closed

infer recursive type (Typescript) #23433

mjbvz opened this issue Apr 16, 2018 · 4 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@mjbvz
Copy link
Contributor

mjbvz commented Apr 16, 2018

From @MikkelSnitker on April 16, 2018 17:25

Hi.
I'm not sure if this is a bug/feature in vscode or in typescript, but recursive types resolves to any?
Giving the code below TestType3 resolves to:

TypeScript Type<{prop1: Type<String>,prop2: Type<any>}>
and not as i expected
TypeScript Type<{prop1: Type<String>,prop2: Type<{subprop: Type<String>}>

I'm not sure if this is a typescript issue, but the compiler catches the error if prop2 does not contain a string property with the name subprop.

type Constructor<T> = { new(...args: any[]): T }
type Type<T> = T & { validate?: (value:any)=>boolean};
type InferTest<T> = T extends Constructor<infer T1> ? Type<T1>: 
T extends Array<Constructor<infer T1>> ? Array<Type<T1>>:
Type<{[K in keyof T]: InferTest<T[K]> }> 

declare function Test1<T>(o: T): InferTest<T>;

const TestType1 = Test1(Number) // Type<Number>;
const TestType2 = Test1([Number]) // Type<Number>[];
const TestType3 = Test1({
    prop1: String,
    prop2: {
        subprop: String
    }
}) // Type<{prop1: Type<String>,prop2: Type<any>}> 

var test4: typeof TestType3 = {
    prop1: "Hello World!",
    prop2: { 
        subprop: 4 
    }
}
  • VSCode Version: 1.22.1
  • OS Version: 10.13.3

Does this issue occur when all extensions are disabled?: Yes

Regards Mikkel

Copied from original issue: microsoft/vscode#47984

@mjbvz mjbvz self-assigned this Apr 16, 2018
@mjbvz mjbvz removed the typescript label Apr 16, 2018
@mjbvz mjbvz removed their assignment Apr 16, 2018
@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Apr 16, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Apr 16, 2018

The recursive type is inferred correctly. the issue is how it is printed when you hover over it.

First type aliases are not types, they are just aliases. in other words the compiler replaces every reference to them with the actually type.
Second when the compiler is serializing the type, and it runs in circularity it uses any to break write that.

@ahejlsberg
Copy link
Member

Inference is actually yielding the expected type

Type<{ prop1: Type<String>, prop2: Type<{ subprop: Type<String> }> }>

The issue is simply that when displaying the type, the compiler only goes a couple of levels deep before substituting an any in order to avoid what might be an infinitely nested type. (In this case the type isn't infinitely nested, but we don't have a heuristic to detect that.)

@MikkelSnitker
Copy link

MikkelSnitker commented Apr 16, 2018

@mhegazy that was what i thought since i got a compile error. is there a way to rewrite (my code red) that so it displays the correct type when hovering?

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

5 participants