Skip to content

Import declarations seems to break namespace compilation #24432

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

Closed
nikkatalnikov opened this issue May 26, 2018 · 7 comments
Closed

Import declarations seems to break namespace compilation #24432

nikkatalnikov opened this issue May 26, 2018 · 7 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@nikkatalnikov
Copy link

TypeScript Version: 2.8.1

Search Terms:
namespace
import
Code

import { Moment } from 'moment'

declare namespace Models {
  export interface ITimeFrame {
    [key: string]: Array<{
      foo: number
      start: Moment
      end: Moment
    }>
  }
}

Expected behavior:
Compiles fine

Actual behavior:
Webstorm spits out an error.

**/service.ts
Error:(12, 17) TS2503: Cannot find namespace 'Models'.
Error:(30, 30) TS2503: Cannot find namespace 'Models'.
Error:(99, 11) TS2503: Cannot find namespace 'Models'.
Error:(100, 6) TS2503: Cannot find namespace 'Models'.
Error:(119, 11) TS2503: Cannot find namespace 'Models'.
Error:(120, 6) TS2503: Cannot find namespace 'Models'.
Error:(146, 11) TS2503: Cannot find namespace 'Models'.
Error:(146, 56) TS2503: Cannot find namespace 'Models'.
Error:(147, 6) TS2503: Cannot find namespace 'Models'.

once import declaration and

start: Moment
end: Moment

are removed, it compiles fine.

also, even import declaration by itself, without

start: Moment
end: Moment

breaks compilation with the same result.

tsconfig:

{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "noImplicitAny": false,
    "alwaysStrict": true,
    "strictNullChecks": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  },
  "exclude":[
    "node_modules",
    "./src/**/*.spec.ts"
  ]
}
@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label May 27, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@nikkatalnikov
Copy link
Author

@mhegazy
why the issue has been closed?

@mhegazy
Copy link
Contributor

mhegazy commented Jun 11, 2018

Modules (i.e. a file with a top lever import or export) introduce their own scope.

@nikkatalnikov
Copy link
Author

ok, so, how can i use imported type in interface declaration? only without namespace pragma?

@mhegazy
Copy link
Contributor

mhegazy commented Jun 11, 2018

@nikkatalnikov
Copy link
Author

nikkatalnikov commented Jun 13, 2018

your example is not relevant, import is not possible due to error msg:

Error:(136, 3) TS1036: Statements are not allowed in ambient contexts.

@nikkatalnikov
Copy link
Author

Apparently, the problem was in moment.js itself

moment/moment#3680 (comment)

creating moment.shim.d.ts:

import * as moment from 'moment'
export as namespace moment
export = moment

and then declaring

  export interface ITimeFrame {
    foo: number
    bar: string
    start: moment.Moment
    end: moment.Moment
  }

works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants