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

Duplicated type leads TSC to pick the wrong one in the *.d.ts file (for implicit return type) #25731

Closed
lorem--ipsum opened this issue Jul 17, 2018 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@lorem--ipsum
Copy link

lorem--ipsum commented Jul 17, 2018

TypeScript Version: ^2.9.2 (also tested with @next with no difference)

Search Terms:
duplicated type, implicit return, type definition file

Code

// component-a/component-a.tsx
export type ComponentAPosition = 'top' | 'bottom' | 'left' | 'right';

export class ComponentA {
  doAThing(value: ComponentAPosition) {}
}

// component-b/component-b.tsx
export type ComponentBPosition = 'top' | 'right' | 'bottom' | 'left';

export class ComponentB {
  doSomething(values: ComponentBPosition) {
    return values;
  }
}

Expected behavior:

// component-b.d.ts
export declare type ComponentBPosition = 'top' | 'right' | 'bottom' | 'left';
export declare class ComponentB {
    doSomething(values: ComponentBPosition): ComponentBPosition;
}

Actual behavior:

// component-b.d.ts
export declare type ComponentBPosition = 'top' | 'right' | 'bottom' | 'left';
export declare class ComponentB {
    doSomething(values: ComponentBPosition): import("../component-a/component-a").ComponentAPosition;
}

Playground Link: Not a playground, but a repo (run npm i && tsc, although the output is in the repo anyway under the build/ directory): https://github.com/lorem--ipsum/tsc-test

Related Issues: -

Thoughts:
The file that gets its type definition is component-b.tsx. It needs to be compiled after component)-a.tsx (I assume the order is alphabetical). The bug does happen when running tsc --watch, but only on the first run, not when component-b.tsx is changed. The returned type is correct when stated explicitly in the source file. It doesn't occur either when the two types (ComponentAPosition and ComponentBPosition aren't strictly similar).

@mhegazy
Copy link
Contributor

mhegazy commented Jul 17, 2018

Type aliases are just that, aliases. the compiler inlines the type alias value for every use of the alias. for visualization purposes (e.g. hover in an IDE, error messages, and declaration file emit), the compiler tries to find a "name" for a union type, and for that it uses the first type alias declaration it saw for this union type. in this case, it is ComponentAPosition. Since the two types are equivalent, there should not be any functional impact to the compiler picking one or the other.

A workaround to avoid this is to give explict type annotations to your functions. e.g.":

export class ComponentB {
    doSomething(values: ComponentBPosition): ComponentBPosition {
        return values;
    }
}

@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jul 17, 2018
@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.

@lorem--ipsum
Copy link
Author

lorem--ipsum commented Jul 28, 2018

Good bot (also, thank you guys for the comments :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants