Skip to content

TypeScript generates invalid import for re-exported types #26985

Description

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

Activity

  1. raymondfeng commented on Sep 9, 2018

    @raymondfeng
    Author

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

  2. mbrowne commented on Nov 19, 2018

    @mbrowne

    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`
    
  3. weswigham commented on Nov 19, 2018

    @weswigham
    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.

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions