简体   繁体   English

使用自定义const枚举的Typescript定义

[英]Typescript definition with custom const enum

I'm trying to create TypeScript definition files for a library. 我正在尝试为库创建TypeScript定义文件。

The library has a method that accepts a parameter of type number , but that parameter can only be a certain set of numbers, so I want to have my definition say it requires an enum type that I created using a const enum . 该库有一个方法接受一个类型为number的参数,但该参数只能是一组特定的数字,所以我想让我的定义说它需要一个我使用const enum创建的枚举类型。

However, when I define my class in a .d.ts , like so: 但是,当我在.d.ts定义我的类时,如下所示:

// definitions/SimpleDefinition.d.ts

/// <reference path="Enums.ts" />

declare class SampleDefinedClass {
    public static SampleMethod(enumArg: Enums.SampleEnum): void;
}

My enum like this: 我的枚举是这样的:

// definitions/Enums.ts

export const enum SampleEnum {
    Item1 = 1,
    Item2 = 2
}

And I have an index.d.ts to tie the 2 together: 我有一个index.d.ts将2绑在一起:

// definitions/index.d.ts

/// <reference path="Enums.ts" />
/// <reference path="SampleDefinition.d.ts" />

The compiler tells me this: 编译器告诉我这个:

../definitions/SampleDefinition.d.ts(4,41): error TS2503: Cannot find namespace 'Enums'.

I have tried adding an import to the top of my SampleDefinition.d.ts , but that resulted in the definition not being properly recognized in my code file. 我尝试在我的SampleDefinition.d.ts的顶部添加一个导入,但这导致我的代码文件中没有正确识别定义。 Though Visual Studio and Visual Studio code won't show an error on the actual import. 虽然Visual Studio和Visual Studio代码不会在实际导入时显示错误。

import Enums = require("./Enums");

Main.ts(6,1): error TS2304: Cannot find name 'SampleDefinedClass'.

I have tried several more thing, like using AMD, and moving files around, but can't seem to get this to work. 我已经尝试了更多的东西,比如使用AMD,移动文件,但似乎无法让它工作。 Is there a way to do this? 有没有办法做到这一点? Or will I have to find another way to do it/give up? 或者我是否必须找到另一种方法来做/放弃?

I've created a GitHub repo with this exact sample. 我用这个确切的样本创建了一个GitHub repo

Your SampleDefinition.d.ts does not have top-level import or export, and Enum.ts does. SampleDefinition.d.ts没有顶级进口或出口,并Enum.ts一样。 That is, Enums is a module, while SampleDefinition is not, but you are trying to use Enums in it. 也就是说, Enums是一个模块,而SampleDefinition不是,但你试图在其中使用Enums Using older (outdated) terminology, SampleDefinition.d.ts is an internal module, and Enums is external module, and you can't mix the two in one application. 使用较旧(过时)的术语, SampleDefinition.d.ts是一个内部模块, Enums是外部模块,您不能将两者混合在一个应用程序中。

There are two ways to make them consistent: 有两种方法可以使它们保持一致:

One way is to make everything internal, without any import/export at top level: 一种方法是将所有内容都设置为内部,而无需在顶层进行任何导入/导出:

fixed Enum.ts : wrap export in a namespace 修复Enum.ts :在namespace包装export

namespace Enums {
    export const enum SampleEnum {
        Item1 = 1,
        Item2 = 2
    }
}

fixed Main.ts - just remove import Enums .. line 修复Main.ts - 只需删除import Enums ..

/// <reference path="../definitions/index.d.ts" />

console.log(Enums.SampleEnum.Item2);
SampleDefinedClass.SampleMethod(Enums.SampleEnum.Item1);

Another way is to turn everything into a module: 另一种方法是将所有东西变成一个模块:

fixed SampleDefinition.ts : use import instead of reference , and export class instead of declaring it: 修复SampleDefinition.ts :使用import而不是referenceexport类而不是声明它:

import Enums = require("./Enums");


export class SampleDefinedClass {
    public static SampleMethod(enumArg: Enums.SampleEnum): void;
}

fixed Main.ts : again, import everything explicitly instead of reference . 修复Main.ts :再次,明确import所有内容而不是reference That way, you don't need definitions/index.d.ts at all: 这样,您根本不需要definitions/index.d.ts

import Enums = require("../definitions/Enums");
import {SampleDefinedClass} from "../definitions/SampleDefinition"


console.log(Enums.SampleEnum.Item2);
SampleDefinedClass.SampleMethod(Enums.SampleEnum.Item1);

Which way to choose is up to you. 选择哪种方式取决于您。 The main difference is that with modules, your app will require module loader at runtime. 主要区别在于,对于模块,您的应用程序将在运行时需要模块加载器。 Without modules, it can be compiled into one combined script file. 没有模块,它可以编译成一个组合的脚本文件。 This matters mostly for browsers (where you have to provide module loader yourself), modular code compiled for node will use require and will work just fine. 这主要适用于浏览器(您必须自己提供模块加载器),为节点编译的模块化代码将使用require并且可以正常工作。

On a definition file you have to export enums with declare . 在定义文件上,您必须使用declare导出枚举。

I looked up some examples from DefinitelyTyped , here is one: 我从DefinitelyTyped中查找了一些示例,这里有一个:

export declare enum EDeflateStrategy {
    DEFAULT_STRATEGY = 0,
    FILTERED = 1,
    HUFFMAN_ONLY = 2,
    RLE = 3,
    FIXED = 4,
}

The problem with your code is that your definition file seems to be looking at a .ts file instead of another .d.ts file. 您的代码的问题是您的定义文件似乎在查看.ts文件而不是另一个.d.ts文件。

If this is your library and it is written in ts you can just add 如果这是你的库并且它是用ts编写的,你可以添加

"declaration": true

on your tsconfig.json file and let tsc generate the definition for you. 在你的tsconfig.json文件上,让tsc为你生成定义。

If it is not you can try to mock things from this library in ts and let the tsc generate declarations for you by enabling this option. 如果不是,您可以尝试在ts中模拟此库中的内容,并让tsc通过启用此选项为您生成声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM