简体   繁体   English

运行时未解析 Typescript 命名空间

[英]Typescript namespace not resolved during runtime

I am very new to Java script module constructs as well as to Typescript.我对 Java 脚本模块构造以及 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我试图从我的文件中引用/导入的打字稿是https://github.com/agea/CmisJS/blob/master/src/cmis.ts

In this ts file a namespace called "cmis" is exported like this在这个 ts 文件中,一个名为“cmis”的命名空间是这样导出的

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我试图在与https://github.com/agea/CmisJS/blob/master/src/cmis.spec.ts相同的另一个文件中引用/导入它

like this import { cmis } from 'cmis';像这样import { cmis } from 'cmis';

I did all the dependency resolution in npm and design time dependency in Visual Studio code is working fine.我在 npm 中完成了所有依赖项解析,并且 Visual Studio 代码中的设计时依赖项工作正常。

My package.json我的 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": "saurav.sarkar1@gmail.com"
  },
  "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但是每当我尝试运行相同的代码时,它都会失败,并出现 TypeError: Cannot read property 'CmisSession' of undefined at

let session = new cmis.CmisSession(url);

index.d.ts in the source library源库中的 index.d.ts

export * from './src/cmis';

So ,node runtime is not able to resolve the cmis namespace imported and vClearly seems to be runtime dependency issue.因此,节点运行时无法解析导入的 cmis 命名空间,并且 vClearly 似乎是运行时依赖问题。

Best Regards, Saurav最好的问候, Saurav

The namespace is not a value in TypeScript so it shouldn't contain any implementation, only type definition.命名空间不是 TypeScript 中的值,所以它不应该包含任何实现,只包含类型定义。

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. Calc 不是函数,只是类型。 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.这就是CmisSession是未定义的。

// 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:实现函数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.这就是命名空间出现并帮助描述代码的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM