Typescript: Imported namespaces aren't recognized in --checkJs

Created on 13 Jun 2017  路  9Comments  路  Source: microsoft/TypeScript

TypeScript Version: nightly (2.5.0-dev.20170613)

Code

test-externclassdef.js

class Foo {
  constructor() {}
}

exports.Foo = Foo;

test-externclass.js

const {Foo} = require('./test-externclassdef');
const Stuff = require('./test-externclassdef');

// this works now
/** @type {Foo} */
let a = new Foo();

// this is still an error
/** @type {Stuff.Foo} */
let b = new Stuff.Foo();

Expected behavior:
No error

Actual behavior:

test-externclass.js(9,12): error TS2503: Cannot find namespace 'Stuff'.

16316 fixes the first kind of import (which I'm really thankful for), but the second kind is still an error.

Bug Fixed

Most helpful comment

+1 on this. I can't annotate JS-doc style to get some intellisense when I'm in a node JS project using commonjs, which is very annoying.

All 9 comments

Actually, the above example works because of type inference, and not because of the annotation.

const {Foo} = require('./test-externclassdef');
// this doesn't work a is still of type any
/** @type {Foo} */
let a;
a = new Foo();

I think I have a similar issue, it might even be the same one but I'm not entirely sure.
I'm trying to reference a type from an npm package that I required in a JSDoc type comment like this:

// some-module.js

declare function SomeModule(opts?: SomeModule.Options);

declare namespace SomeModule {
  interface Options {
    enabled?: boolean
  }
}

export = SomeModule;
// my-code.js

const SomeModule = require('./some-module');

// Causes "Cannot find namespace 'SomeModule'" error
/** @type {SomeModule.Options} */
const options = {
  enabled: true
};

Would the fix for this issue help my case too?

@lumaxis yes, I believe so.

I didn't want to open up another issue but this seems like it could be related to the issue I'm having.

import will load a module's .d.ts file but require wont. I can't use import on NodeJS and have to use require

Works with --checkJS, but won't run in NodeJS

import moment from 'moment';
/**
 * @return {moment.Moment}
 */
function getTime() {
  return moment();
}

Doesn't work with --checkJS, but works with NodeJS:

const moment = require('moment');

/**
 * @return {moment.Moment}
 */
function getTime() {
  return moment();
}

Error: Cannot find namespace 'moment'.

+1 on this. I can't annotate JS-doc style to get some intellisense when I'm in a node JS project using commonjs, which is very annoying.

Will this also allow to import an interface from a ts file?
Right now I can't see how const ExampleInterface = require('./my-interface.ts'); could work without transpiling.

What do you think of adding a name attribute for tripple slash directives like in the following example?

/// <reference path ="./my-interface.ts" name="ExampleInterface" />

Please see also #19547 for a more detailed example

@cesarvarela You appear to be wrong; TypeScript (at least the latest version thereof) seems to correctly deduce a is type Foo, and report errors if I assign other objects to a.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bgrieder picture bgrieder  路  3Comments

uber5001 picture uber5001  路  3Comments

jbondc picture jbondc  路  3Comments

kyasbal-1994 picture kyasbal-1994  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments