Node.js 在命名空间中找不到名称
我试图在typescript中分离接口和实现,所以我选择使用Node.js 在命名空间中找不到名称,node.js,typescript,namespaces,es6-modules,Node.js,Typescript,Namespaces,Es6 Modules,我试图在typescript中分离接口和实现,所以我选择使用module特性。但是,即使在使用时,我也始终收到无法找到名称的消息。这是我的密码: IUserService.ts namespace Service { export interface IUserService { login(username: string, password: string): void; } } /// <reference path="./IUserService.
module
特性。但是,即使在使用
时,我也始终收到无法找到名称的消息。这是我的密码:
IUserService.ts
namespace Service {
export interface IUserService {
login(username: string, password: string): void;
}
}
/// <reference path="./IUserService.ts" />
namespace Service {
export class UserService implements IUserService {
constructor() {}
}
UserService.ts
namespace Service {
export interface IUserService {
login(username: string, password: string): void;
}
}
/// <reference path="./IUserService.ts" />
namespace Service {
export class UserService implements IUserService {
constructor() {}
}
//
命名空间服务{
导出类UserService实现IUserService{
构造函数(){}
}
然后tsc总是抱怨在UserService.ts中找不到名称IUserService。我遵循文档中关于名称空间的说明,但它对我不起作用。对此应该如何解决?两个建议:
- 不要使用
//
语法
- 不要同时使用名称空间和模块。Node.js已经提供了模块,所以您不需要名称空间
以下是一个解决方案:
// IUserService.d.ts
export interface IUserService {
login(username: string, password: string): void;
}
// UserService.ts
import { IUserService } from "./IUserService";
export class UserService implements IUserService {
constructor() {
}
login(username: string, password: string) {
}
}
您必须进行定义。//
语句被配置文件(tsconfig.json)替换(“轻量级可移植项目”部分)
相关:和。但是2019年持续教授//
包含和名称空间
的糟糕教程在哪里?这是官方教程,您的代码将在浏览器或Node.js上执行?如果这是浏览器,您是否使用Webpack或Require.js或其他工具?这将在Node.js上执行。