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

Imported namespaces aren't recognized in --checkJs #16489

Closed
Zarel opened this issue Jun 13, 2017 · 9 comments
Closed

Imported namespaces aren't recognized in --checkJs #16489

Zarel opened this issue Jun 13, 2017 · 9 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@Zarel
Copy link

Zarel commented Jun 13, 2017

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.

@mhegazy mhegazy added the Bug A bug in TypeScript label Jun 13, 2017
@mhegazy mhegazy added this to the TypeScript 2.5 milestone Jun 13, 2017
@mhegazy mhegazy added the Salsa label Jun 13, 2017
@cesarvarela
Copy link

cesarvarela commented Jun 22, 2017

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();

@mhegazy mhegazy modified the milestones: Future, TypeScript 2.6 Aug 31, 2017
@lumaxis
Copy link

lumaxis commented Sep 4, 2017

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?

@DanielRosenwasser
Copy link
Member

@lumaxis yes, I believe so.

@clshortfuse
Copy link

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'.

@liminzhu
Copy link
Member

+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.

@jantimon
Copy link

jantimon commented Nov 21, 2017

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

@mhegazy
Copy link
Contributor

mhegazy commented Nov 21, 2017

I think you are looking for #14377

@Zarel
Copy link
Author

Zarel commented Jan 13, 2018

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

@mhegazy
Copy link
Contributor

mhegazy commented Mar 9, 2018

Should be fixed by #21974 and #22161

@mhegazy mhegazy closed this as completed Mar 9, 2018
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Mar 9, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

10 participants