首页 > 解决方案 > 为什么 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。

标签: typescript

解决方案


推荐阅读