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 generates invalid import for re-exported types #26985

Closed
raymondfeng opened this issue Sep 9, 2018 · 3 comments · Fixed by #27340
Closed

TypeScript generates invalid import for re-exported types #26985

raymondfeng opened this issue Sep 9, 2018 · 3 comments · Fixed by #27340
Assignees
Labels
Bug A bug in TypeScript

Comments

@raymondfeng
Copy link

raymondfeng commented Sep 9, 2018

The issue was reported on #24941 (comment). It was initially discovered in loopbackio/loopback-next#1643.

In this case, MetadataAccessor is exported from @loopback/metadata and re-exported by @loopback/context. Please note that @loopback/repository-json-schema module imports MetadataAccessor from @loopback/context. It leads to incorrectly generated code.

TypeScript Version: 3.0.x and up

Search Terms: TypeScript import types

Code

https://github.com/strongloop/loopback-next/blob/5b27323ba420bfc500fdf9f2762e7292b96ffa0f/packages/repository-json-schema/src/keys.ts

// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.

Expected behavior:

TypeScript generates the following code:

import { MetadataAccessor } from '@loopback/context';
import { JSONSchema6 as JSONSchema } from 'json-schema';
/**
 * Metadata key used to set or retrieve repository JSON Schema
 */
export declare const JSON_SCHEMA_KEY: MetadataAccessor<JSONSchema, import("@loopback/context").DecoratorType>;

Actual behavior:

TypeScript generates invalid code as follows:

import { MetadataAccessor } from '@loopback/context';
import { JSONSchema6 as JSONSchema } from 'json-schema';
/**
 * Metadata key used to set or retrieve repository JSON Schema
 */
export declare const JSON_SCHEMA_KEY: MetadataAccessor<JSONSchema, import("../context/node_modules/@loopback/metadata/src/types").DecoratorType>;

Playground Link:

Related Issues:
#24874

@raymondfeng
Copy link
Author

@weswigham I created a simpler project to reproduce the problem - https://github.com/raymondfeng/ts-26985.

raymondfeng added a commit to loopbackio/loopback-next that referenced this issue Sep 10, 2018
raymondfeng added a commit to loopbackio/loopback-next that referenced this issue Sep 10, 2018
raymondfeng added a commit to loopbackio/loopback-next that referenced this issue Sep 10, 2018
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Sep 17, 2018
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.2 milestone Sep 17, 2018
raymondfeng added a commit to loopbackio/loopback-next that referenced this issue Sep 28, 2018
raymondfeng added a commit to loopbackio/loopback-next that referenced this issue Sep 28, 2018
raymondfeng added a commit to loopbackio/loopback-next that referenced this issue Sep 28, 2018
@mbrowne
Copy link

mbrowne commented Nov 19, 2018

Is it expected behavior that the PR that was just merged for this (#27340) seems to break certain export const statements in a project with "declaration": true set in tsconfig?

For example:

error TS2742: The inferred type of 'Wrapper' cannot be named without a reference to '../../../../../../../../../../Users/mbrowne/GoogleDrive/www/artnet-styled/styled/node_modules/@types/styled-components'. This is likely not portable. A type annotation is necessary.

13 export const Wrapper = styled.div`

@weswigham
Copy link
Member

Yup. When that error occurs, we would otherwise have written a reference to the module specifier in the message (and you see how bad that is, right?). It's better to require an annotation when we detect a required path like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
5 participants