0
votes

I am very new to Java script module constructs as well as to Typescript.

I am trying to import an namespace and all the exported members under that.

Typescript i am trying to refer/import from my file is https://github.com/agea/CmisJS/blob/master/src/cmis.ts

In this ts file a namespace called "cmis" is exported like this

export namespace cmis {

I am trying to refer/import this in another file same as in https://github.com/agea/CmisJS/blob/master/src/cmis.spec.ts

like this import { cmis } from 'cmis';

I did all the dependency resolution in npm and design time dependency in Visual Studio code is working fine.

My package.json

   {
  "title": "sdm-nodejsclient",
  "name": "cmsdm-nodejsclientis",
  "version": "0.0.1",
  "description": "a CMIS client library written in Typescript for node and the browser",
  "author": {
    "name": "Saurav Sarkar",
    "email": "[email protected]"
  },
  "dependencies": {
    "@types/jest": "^25.1.3",
    "@types/node": "^13.7.4",
    "chai": "^4.2.0",
    "cmis": "~1.0.2",
    "cross-fetch": "~1.1.1",
    "es6-promise": "~4.2.4",
    "isomorphic-base64": "~1.0.2",
    "isomorphic-form-data": "~1.0.0",
    "jest": "^25.1.0",
    "mocha": "^7.0.1",
    "ts-jest": "^25.2.1",
    "url-search-params-polyfill": "~2.0.2"
  },
  "devDependencies": {
    "@types/chai": "~4.1.2",
    "@types/mocha": "~2.2.48",
    "@types/node": "~8.5.2",
    "chai": "~4.1.2",
    "cmis": "~1.0.2",
    "mocha": "~5.0.0",
    "ts-loader": "~2.0.3",
    "ts-node": "~3.0.6",
    "typedoc": "~0.10.0",
    "typescript": "~2.7.1",
    "uglify-js": "~3.3.7",
    "uglifyjs-webpack-plugin": "~1.1.8",
    "webpack": "~3.10.0"
  }
}

But whenever i try to run the same code, it is failing with TypeError: Cannot read property 'CmisSession' of undefined at

let session = new cmis.CmisSession(url);

index.d.ts in the source library

export * from './src/cmis';

So ,node runtime is not able to resolve the cmis namespace imported and vClearly seems to be runtime dependency issue.

Best Regards, Saurav

1

1 Answers

0
votes

The namespace is not a value in TypeScript so it shouldn't contain any implementation, only type definition.

For example:

// types.d.ts
// This is ok
declare namespace MySpace {
  type Calc = (a: number, b: number) => number;
}

Calc is not a function, just type. We can use types to say compiler what are things (for example functions). But we can't execute types. Types is something like meta data and it removes from code during compilation. This is wy CmisSession is undefined.

// types.d.ts
// This is wrong! Type definition contains implementation.
declare namespace MySpace {
  type Calc = (a: number, b: number) => { return a + b; };
}

Implementation should not mixed with type definition. Implementation function calc:

// index.ts
const calc: MySpace.Calc = (a, b) => a + b;

Don't need to declare types inside implementation. This is why namespaces comes and helps describe the code.