相关疑难解决方法(0)

Typescript es6导入模块"文件不是模块错误"

我正在使用带有es6模块语法的typescript 1.6.

我的文件是:

test.ts:

module App {
  export class SomeClass {
    getName(): string {
      return 'name';
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

main.ts:

import App from './test';

var a = new App.SomeClass();
Run Code Online (Sandbox Code Playgroud)

当我尝试编译该main.ts文件时,我收到此错误:

错误TS2306:文件'test.ts'不是模块.

我怎么能做到这一点?

javascript typescript ecmascript-6

101
推荐指数
4
解决办法
11万
查看次数

`declare namespace`和`declare module`有什么区别?

阅读本手册和本引文后:

值得注意的是,在TypeScript 1.5中,命名法已经改变."内部模块"现在是"命名空间"."外部模块"现在只是"模块"

我下的印象是declare module不再使用,被取代declare namespace,但探索的时候node_modules\@types\node\index.d.ts,我可以看到,无论是declare moduledeclare namespace使用:

declare namespace NodeJS {
    export var Console: {
        prototype: Console;
        new(stdout: WritableStream, stderr?: WritableStream): Console;
    }
...

declare module "buffer" {
    export var INSPECT_MAX_BYTES: number;
    var BuffType: typeof Buffer;
    var SlowBuffType: typeof SlowBuffer;
    export { BuffType as Buffer, SlowBuffType as SlowBuffer };
}
Run Code Online (Sandbox Code Playgroud)

为什么这样?有什么不同?

根据我的理解,外部模块(ES6模块)不会在这里发挥作用.

typescript

15
推荐指数
1
解决办法
3373
查看次数

TypeScript | 模块没有导出成员

遵循这些相当基本的指示,但这似乎不起作用.

/sf/answers/2512630921/

概观

我想用import { Type } from "Module"而不是/// <reference path="..." />

结构体

-app\
  -ViewModel.ts
  -Program.ts
Run Code Online (Sandbox Code Playgroud)

ViewModel.ts

export module Demo {
    export class ViewModel {
        constructor(public test: string) {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Program.ts

import { ViewModel } from "ViewModel";
Run Code Online (Sandbox Code Playgroud)

模块'C:/ DemoApp/app/ViewModel'没有导出成员'ViewModel'.

和...

--outFile只支持'amd'和'system'模块.

目标

我希望能够引用依赖项,以便它们按顺序编译为单个文件.

请帮忙!

注意:如果我添加,"module": "system"我仍然会得到上述错误中的第一个.

注意:根据第一个解决方案,我不想丢失名称空间!

import typescript

13
推荐指数
1
解决办法
3万
查看次数

如何添加Jasmine自定义匹配器Typescript定义?

我一直在环顾 四周,这个问题似乎是一个反复出现的问题.但是,我发现的解决方案似乎都不适合我.

使用以下内容:

{
  "typescript": "2.3.2",
  "jasmine-core": "2.6.1",
  "@types/jasmine": "2.5.47"
}
Run Code Online (Sandbox Code Playgroud)

我不能让Typescript合并包含我的自定义匹配器定义的名称空间声明.

添加这个:

declare namespace jasmine {
  interface Matchers<T> {
    toBeAnyOf(expected: jasmine.Expected<T>, expectationFailOutput?: any): boolean;
  }
}
Run Code Online (Sandbox Code Playgroud)

隐藏之前声明的所有其他类型jasmine.编译器输出错误,例如:

[ts] Namespace 'jasmine' has no exported member 'CustomMatcherFactories'
[ts] Namespace 'jasmine' has no exported member 'CustomMatcher'.
Run Code Online (Sandbox Code Playgroud)

是否有任何正确的方法来添加自定义匹配器并使其与Typescript很好地协作?

tslint如果您使用tslint:recommended规则集,还有一个问题.这些规则不允许使用namespacemodule关键字,因此我必须禁用linter(或更改"no-namespace"规则)才能尝试这一点.不确定如果"不推荐",如何扩展定义.

javascript types jasmine typescript angular

10
推荐指数
1
解决办法
1549
查看次数

angular 2命名空间模型

如何在角度2中使用模型类?

这是一个例子

模型

namespace model.car {
    export class CoolCar {
        Id: string;
        Name: string;

        constructor(){
        }
    }

    export class NiceCar {
        Id: string;
        Name: string;

        constructor(){
        }
    }
}

namespace model.bike {
    export class AwesomeBike {
        Id: string;
        Name: string;

        constructor(){
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想在我的课堂上使用它们

var car=new model.car.CoolCar();
Run Code Online (Sandbox Code Playgroud)

但是当我在浏览器中运行它时,我收到一个错误

"ReferenceError: 'model' is undefined"
Run Code Online (Sandbox Code Playgroud)

我试着像这样导入模型类

import {CoolCar} from '../model/car/CoolCar'
Run Code Online (Sandbox Code Playgroud)

但后来我在VS2015中遇到错误:

File "c:/...../model/car/CoolCar.ts is" no module
Run Code Online (Sandbox Code Playgroud)

有人可以帮我吗?

托比亚斯

typescript angular

7
推荐指数
1
解决办法
1万
查看次数

命名空间vs模块与打字稿中的打字?

有人可以用一个例子来解释这些概念之间的区别,我真的很困惑,当它们都服务于同一目的时,有三个这样的用途是什么?

typescript

7
推荐指数
1
解决办法
1722
查看次数

使用TypeScript将导入导出为命名空间

我的问题与此问题几乎相同:https: //github.com/Microsoft/TypeScript/issues/4529

说我有这个:

//exported imports
export {ISumanOpts, IGlobalSumanObj} from 'suman-types/dts/global';
export {ITestCaseParam} from 'suman-types/dts/test-suite';
export {IHookParam} from 'suman-types/dts/test-suite';
export {IDescribeFn} from 'suman-types/dts/describe';
export {ItFn, ITestDataObj} from 'suman-types/dts/it';
export {IBeforeFn} from 'suman-types/dts/before';
export {IBeforeEachFn} from 'suman-types/dts/before-each';
export {IAfterFn} from 'suman-types/dts/after';
export {IAfterEachFn} from 'suman-types/dts/after-each';
export {DefineObjectContext as IDefObjCtx} from "./test-suite-helpers/define-options-classes";
export {DefineObjectTestCase as IDefObjTestCase} from "./test-suite-helpers/define-options-classes";
export {DefineObjectAllHook as IDefObjAllHook} from "./test-suite-helpers/define-options-classes";
export {DefineObjectEachHook as IDefObjEachHook} from "./test-suite-helpers/define-options-classes";


export namespace s {

  // ! I want to move all …
Run Code Online (Sandbox Code Playgroud)

typescript typescript2.0 suman

7
推荐指数
2
解决办法
4211
查看次数

为什么 ES2015 模块语法优于自定义 TypeScript 命名空间?

来自typescript-eslint 文档

自定义 TypeScript 模块 ( module foo {}) 和命名空间 ( namespace foo {}) 被认为是过时的组织 TypeScript 代码的方式。现在首选 ES2015 模块语法 ( import/ export)。

来自 Typescripts文档

模块可以包含代码和声明。

模块还依赖于模块加载器(例如 CommonJs/Require.js)或支持 ES 模块的运行时。模块提供更好的代码重用、更强的隔离和更好的捆绑工具支持。

还值得注意的是,对于 Node.js 应用程序,模块是默认设置,我们建议在现代代码中使用模块而不是命名空间。

我这个问题的目标是找出为什么模块语法优于命名空间。

到目前为止,这是我一直在做的事情:

PROJECT_TYPES.d.ts

我有多个d.ts文件,其中声明了包含我在项目源文件中使用的类型的命名空间。

declare namespace MY_NAMESPACE {
  type FOO = "FOO"
}
Run Code Online (Sandbox Code Playgroud)

这样做,我可以在项目的任何源文件中执行以下操作:

一些文件.ts

const foo: MY_NAMESPACE.FOO =  "FOO";
Run Code Online (Sandbox Code Playgroud)

它无需任何import/即可工作export

笔记:

  • 我正在使用d.ts文件,因为这些文件不包含任何import/export代码。
  • 我使用declare namespace而不是仅仅namespace …

namespaces module typescript typescript-typings .d.ts

7
推荐指数
1
解决办法
7393
查看次数

在类型定义文件中定义类型与在普通类型文件中定义类型的优点

对于应用程序代码,是否建议将 Redux 存储类型放在state-slice.d.ts文件中?

\n

我继承了一个代码库,其中所有核心类型都放置在.d.ts定义文件中,但这与我\xe2\x80\x99ve 理解定义文件的作用的方式相反。

\n

我的理解是,定义文件适用于您\xe2\x80\x99编写库时(这是作者集中接口来定义我们向消费者公开的库的API的一个方便的地方),但不适合对于应用程序代码,因为数据可能会快速变化。

\n

也就是说,我\xe2\x80\x99无法对.d.ts文件本身提出一个很好的论据 - 从功能的角度来看,我不\xe2\x80\x99t看到引用类型的区别

\n
declare namespace Domain {\n  interface State {\n    // ...\n  }\n} \n
Run Code Online (Sandbox Code Playgroud)\n

从定义文件中,或从具有以下内容的文件中显式导入文件

\n
export interface ReduxState {}\n
Run Code Online (Sandbox Code Playgroud)\n

添加更多上下文,因为这是一个有点特殊的问题 - I\xe2\x80\x99m 目前正在开发一个新项目,该项目需要从我继承的现有代码库中共享这些核心类型,并且 I\xe2\x80\ x99m 尝试将这些类型移至单独的包中。

\n

不幸的是,我\xe2\x80\x99在导入和导出这些类型时遇到了问题 - 似乎我必须(注意 s export

\n
/* package A */\nexport declare namespace StateA { }\n\n/* package B */\nimport { StateA } from 'packageA/StateA' \n\nconst stateA: StateA = {};\n
Run Code Online (Sandbox Code Playgroud)\n

而不是 TypeScript 自动拾取它。

\n …

types typescript redux react-redux

7
推荐指数
1
解决办法
1102
查看次数

TypeScript:将 React 组件导出为命名空间的一部分

我有以下文件:

自动建议组件.tsx:

import * as React from 'react';

interface AutosuggestProps {
    ...
}    

interface AutosuggestState {
   ...
}

export default class Autosuggest extends React.Component<AutosuggestProps, AutosuggestState> {
    ...
}
Run Code Online (Sandbox Code Playgroud)

我想在 ConsumerComponent.tsx 中导入这样的 Autosuggest 组件:

import Autosuggest = Components.AutoSuggest;
Run Code Online (Sandbox Code Playgroud)

如何导出 AutosuggestComponent.tsx 才能完成这项工作?

我尝试创建一个 Autosuggest.ts,如下所示:

import AutosuggestComponent from './AutosuggestComponent';

namespace Components {
    export const Autosuggest = AutosuggestComponent;
}
Run Code Online (Sandbox Code Playgroud)

这是行不通的。然后 ConsumerComponent 无法找到命名空间“Components”。然而,这有效:

// import AutosuggestComponent from './AutosuggestComponent';

namespace Components {
    export const Autosuggest = { dummyValue: "test" }
}
Run Code Online (Sandbox Code Playgroud)

一旦我注释掉导入,ConsumerComponent 就能够找到名称空间。为什么?

有什么办法可以解决这个问题吗?

typescript reactjs

5
推荐指数
1
解决办法
2万
查看次数

TypeScript 模块导入和 WebPack

我在让 WebPack 为用 TypeScript 编写的项目注入导入的依赖项时遇到了一些麻烦。我的第一个问题是让 TypeScript 识别导入的模块。

我有一个header.ts文件,它声明了一个嵌套在vi.input下的模块,并导出了一个VIInputDirective类。在main.ts文件中,我尝试从header.ts文件导入导出的VIInputDirective类,但似乎无法让 TypeScript 识别它。

头文件

module vi.input.header {
  import IDirective = angular.IDirective;

  export class VIInputDirective implements IDirective {
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

主文件

import {VIInputDirective} from "./header.ts"; // Nothing imported; Cannot Resolve issue
VIInputDirective.whatever(); // Does not work
Run Code Online (Sandbox Code Playgroud)

材料.dt.s

declare module vi.input {
  ...
}
Run Code Online (Sandbox Code Playgroud)

如果我import {VIInputDirective} from "./header.ts";main.ts文件中交换import VIMHeaderDirective = vi.input.header.VIInputDirective;它工作正常,但是 webpack on transpile/inject 给我以下错误:

VM1469:1Uncaught …
Run Code Online (Sandbox Code Playgroud)

javascript typescript webpack

3
推荐指数
1
解决办法
2757
查看次数

打字稿 - 应用程序范围状态(Singleton?)

我是打字稿的初学者,我已经阅读了很多关于打字稿和单身人士的文章,我仍然可以使用它.

我有同样的问题: Node.js和Typescript Singleton模式:看起来单身用户之间不共享singleton.

我也读过这篇文章:https://fullstack-developer.academy/singleton-pattern-in-typescript/ 和这一篇:http://www.codebelt.com/typescript/typescript-singleton-pattern/

最后,当我从一个模块转到另一个模块时,它看起来像我的单例类始终处于默认状态.在调用时getInstance(),因为值设置为new Path()对我来说似乎很明显,单例始终处于默认状态,但在许多源中(如前两个提供的那样)是这样做的方法.

我究竟做错了什么 ?谢谢.

这是我的单身人士(Path.ts):

class Path{
    private static _instance: Path = new Path();
    private _nodes: NodeModel[];
    private _links: LinkModel[];

    public static getInstance(): Path{
        if(Path._instance)
            return Path._instance;
        else
            throw new Error("Path is not initialized.");
    }

    public setPath(nodes: NodeModel[], links: LinkModel[]){
        this._nodes = nodes;
        this._links = links;
    }

    public nodes(){ return this._nodes; }
  [...]
}
export = Path;
Run Code Online (Sandbox Code Playgroud)

PathDefinition.ts

module PathDefinition{
    export function defaultDefinition(){ …
Run Code Online (Sandbox Code Playgroud)

typescript

1
推荐指数
1
解决办法
1448
查看次数