typescript - 为什么 TS 会自动检测我的类声明而不是我的类型别名?
问题描述
我有一个带有export declare type Something = /**types here*/
. 我Something
在另一个 ts 文件中使用类型别名。
我的目标是让 TS 接受,而不是像对模块、类、接口和变量const myVar: Something = /**instance of Something*/
那样显示弯曲的红线。Cannot find name 'Something'
我的情况:
我有这个声明文件,./declarations/Emailer.d.ts
:
export declare /**taking out export or declare doesn't work */ type EmailOptions = { host?: string; port?: string | number ; secure?: boolean; user?: string; password?: string; from?: string; project_id?: string | number; email_id?: string | number; }
export declare class Emailer {
transporter: any; // Nodemailer.prototype.createTransport(options: EmailOptions)
from: string;
project_id: number;
email_id: number;
constructor(options: EmailOptions);
}
以及 TS 文件 Emailer.ts:
const nodemailer = require('nodemailer');
// import { EmailOptions } from './declarations/Emailer'; // It works if I add this
class Emailer {
transporter;
from;
project_id;
email_id;
constructor(options: EmailOptions /** The 'Cannot find name' error is here */) {
//...
}
TS 如何自动检测我的类声明而不是类型别名?
为了弄清楚这一点,我研究了一般的类型别名、路径中三重指令的使用、访问修饰符、库结构和声明合并。
我怀疑我对 TS 中的模块和命名空间的概念很弱,并将继续研究。
我正在使用 TSC 版本 3.5.2 和 VS Code 1.49.3。
解决方案
推荐阅读
- android - 如何为android的布局组件添加灵活的大小
- html - 我想在输入文本上做动画
- excel - VBA 嵌套 FOR 循环适用于第一次出现但忽略以下循环
- java - 如何设置 Firebase 实时数据库的双重查询?
- angular - RxJs 操作符避免多次订阅,有循环
- c++builder - 从 TCustomGrid 覆盖 DrawCell > 为什么受保护?
- c# - 是否有内置方法可以在没有 IOptions 的情况下进行配置模型绑定验证?
- email - 如何在机器人运行结束时通过电子邮件发送异常日志
- arrays - 如何将 CSV 数据导入多个数组并通过 VBA 中的函数或子返回多个数组?
- performance - 提高 GAS 中的图表更新性能