Skip to content

Importing types from a js module doesn't work #28660

Closed
@zombie

Description

@zombie

TypeScript Version: 3.2.0-rc

Code

Example from TypeScript 2.9 release notes:

// module.js

class Pet {
  get name() {
    return "";
  }
}

module.exports = {Pet};
// global-script.ts

// error TS2694: Namespace `"module".export=` has no exported member `Pet`.

function adopt(p: import("./module").Pet) {
  console.log(`Adopting ${p.name}...`);
}

// This works, though it's far from ideal, and not always applicable.

let module: typeof import("./module");

function adopt2(p: typeof module.Pet.prototype) {
  console.log(`Adopting ${p.name}...`);
}
// a.js

// error TS2694: Namespace `"module".export=` has no exported member `Pet`.

/**
 * @param {import("./module").Pet} p
 */
function walk(p) {
  console.log(`Walking ${p.name}...`);
}

// Works, showing that type information is exported/preserved on some level.

/**
 * @typedef {import("./module")} module
 * @param {typeof module.Pet.prototype} p
 */
function walk2(p) {
  console.log(`Walking ${p.name}...`);
}

Typecheck using:

tsc -t es2017 --checkjs --allowjs --noemit a.js global-script.ts

Expected behavior:
Type annotation should work and not error out.

Actual behavior:
error TS2694: Namespace "module".export= has no exported member Pet.

Related Issues:
Issue #23936 seems possibly related, but not sure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: JavaScriptThe issue relates to JavaScript specifically

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions