[go: up one dir, main page]

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

[WIP] Fix importing generic types #312

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

0x53A
Copy link
Contributor
@0x53A 0x53A commented Oct 3, 2019

If I have two files where I export and import a generic type

declare type IGeneric<T> = { value: T };
export { IGeneric }

and

import { IGeneric } from './#xxx-generic-aliases.2';

type IGenericAlias<T> = IGeneric<T>;

declare function f(a: IGeneric<number>, b: IGenericAlias<number>);

export { IGeneric, IGenericAlias, f };

then it currently generates invalid F# code.

@0x53A 0x53A changed the title Fix importing generic types WIP: Fix importing generic types Oct 3, 2019
@0x53A
Copy link
Contributor Author
0x53A commented Oct 3, 2019

I was following https://stackoverflow.com/questions/50526710/typescript-compiler-api-get-type-of-imported-names for how to get the type of the imported object.

(tl;dr:

let symbol = checker.getSymbolAtLocation(imp.name)
let type = checker.getDeclaredTypeOfSymbol(symbol);

The change in CompilerHost was neccessary because without it it would always return undefined. With this change it returns a valid type.

@0x53A 0x53A changed the title WIP: Fix importing generic types Fix importing generic types Oct 4, 2019
@0x53A 0x53A changed the title Fix importing generic types [Ready4Review] Fix importing generic types Oct 4, 2019
@0x53A
Copy link
Contributor Author
0x53A commented Oct 4, 2019

This actually fixes two things:

  • the mentioned issue when importing generics
  • default exports were not imported

see https://github.com/microsoft/TypeScript/blob/9a62db2b5c5a305139d18f5f25c725f0779493cd/src/compiler/types.ts#L1807-L1817

If I did import d, { a } from 'x' then only a was imported, d was ignored. Now with this change, both should be correctly imported.

@0x53A
Copy link
Contributor Author
0x53A commented Oct 4, 2019

There is one pending issue with importing generics - classes and interfaces work, aliases don't.

example:

file 1

declare interface IGenericInterface<T> { value: T }
declare class GenericClass<T> { value: T }
declare type GenericAlias<T> = { value : T }
export { IGenericInterface, GenericClass, GenericAlias }

file 2:

import { IGenericInterface, GenericClass, GenericAlias } from './core/other';

will generate

type IGenericInterface<'T> = __core_other.IGenericInterface<'T>
type GenericClass<'T> = __core_other.GenericClass<'T>
type GenericAlias = __core_other.GenericAlias

when the last line should be

type GenericAlias<'T> = __core_other.GenericAlias<'T>

But I can't get them the same way, getDeclaredTypeOfSymbol returns undefined for the alias.

Would you pull this anyway? Even though it is incomplete, it's still better than before :D

@humhei
Copy link
Contributor
humhei commented Oct 5, 2019

image

@0x53A
Copy link
Contributor Author
0x53A commented Oct 7, 2019

I've merged master - I had wanted to do a few smaller PRs instead of one big one, but that kinda backfired.

Also I found another issue: default exports are not correctly exported, so it isn't 100% finished.

I'll try to complete that and add a unit test.

@0x53A 0x53A changed the title [Ready4Review] Fix importing generic types [WIP] Fix importing generic types Oct 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants