我有一些代码:
baseTypes.ts
export namespace Living.Things {
export class Animal {
move() { /* ... */ }
}
export class Plant {
photosynthesize() { /* ... */ }
}
}
Run Code Online (Sandbox Code Playgroud)
dog.ts
import b = require('./baseTypes');
export namespace Living.Things {
// Error, can't find name 'Animal', ??
export class Dog extends Animal {
woof() { }
}
}
Run Code Online (Sandbox Code Playgroud)
tree.ts
// Error, can't use the same name twice, ??
import b = require('./baseTypes');
import b = require('./dogs');
namespace Living.Things {
// Why do …Run Code Online (Sandbox Code Playgroud) 我正在使用TypeScript和Express/ Node.js.
对于使用模块,TypeScript手册显示以下语法:
import express = require('express');
但typescript.d.ts文件也显示:
import * as express from "express";
我还搜索了MSDN博客,但找不到任何东西.
从2016年初开始哪一个更正确?如果有的话,两者之间有什么区别?
哪个是查找有关最新语法的信息的最佳来源,以便将来可以找到这些信息?
我有一个Angular2项目,我需要在其中输入要在我的打字稿中使用的javascript文件。
我在app / js / d3gantt.js中有一个javascript文件,其中包含的单个功能gantt = function()。
gantt = function() {
//Does lots of stuff
return gantt;
};
Run Code Online (Sandbox Code Playgroud)
然后,在同一个文件夹中有一个名为d3gannt.d.ts的定义文件,如下所示:
declare module 'd3Gantt' {
export module d3Gantt {
export function gantt(): any;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我在我的组件中引用为
import * as d3Gantt from '../app/js/d3gantt';
但是,我收到错误消息,指出 File 'c:/Projects/RepackLog/RepackLog/RepackLog/app/js/d3gantt.d.ts' is not a module
我是否缺少一些必要的东西才能正常拾取我的文件?
谢谢,
我正在尝试将 index.ts 导入到具有其他导入的子文件夹中。但我不断收到打字稿错误。
完整仓库: https: //github.com/Shavindra/webpack-react-sw
(5,32): error TS2306: File 'C:/../src/reducers/index.ts' is not a module.
我不太确定我在这里做错了什么。我使用的是 TS 2.4.1。我尝试重新启动计算机/VSCode,但似乎没有任何效果:-|
// ./src/reducers/counter.reducer.ts
export const counterReducer = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
};
// ./src/reducers/index.ts
export * from './counter.reducer';
// ./src/app.ts
import * as React from 'react';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { createStore } …Run Code Online (Sandbox Code Playgroud) 我刚刚将我的第一个库包发布到 NPM,并尝试在应用程序项目中使用它。
它是用 typescript 编写的,项目构建正常,并已发布到 NPM。但随后尝试使用它失败了,因为显然它不是一个有效的模块。
index.ts 文件中设置模块导出(afaik)的代码是:
const initByClass = (widgetBindings: WidgetClassBinding[], h: any, render: any) => {
for (const widgetBinding of widgetBindings) {
setupWidget(widgetBinding, h, render);
}
};
module.exports = {
initByClass
};
Run Code Online (Sandbox Code Playgroud)
库中的 tsconfig.json 文件是:
{
"compilerOptions": {
"esModuleInterop": true,
"target": "ES6",
"module": "ES6",
"moduleResolution": "node",
"declaration": true,
"outDir": "./lib",
"strict": true
},
"include": ["src"],
"exclude": ["node_modules", "**/__tests__/*"]
}
Run Code Online (Sandbox Code Playgroud)
在我的应用程序代码中,我已将包添加到 package.json (并运行 npm update),并尝试使用以下命令将其导入应用程序入口文件中:
import { initByClass } from "widgety";
Run Code Online (Sandbox Code Playgroud)
但它给了我一个错误:
TS2306:文件“/var/app/app/node_modules/widgety/src/index.ts”不是模块。
我需要更改什么才能使代码可以导入到另一个打字稿项目中?
如果它们有用,所有项目文件: https: //github.com/Danack/widgety/ 以及 …
typescript ×5
javascript ×4
angular ×1
export ×1
express ×1
import ×1
module ×1
node.js ×1
npm ×1
reactjs ×1