TypeScript 中的 Typedef 错误:“预期的 variableDeclarator”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26654663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:56:22  来源:igfitidea点击:

Typedef in TypeScript error: 'expected variableDeclarator'

typescripttslint

提问by homaxto

Running tslint on my code I get this error:

在我的代码上运行 tslint 我收到这个错误:

expected variableDeclarator: 'getCode' to have a typedef.

for a TypeScript file:

对于打字稿文件:

export var getCode = function (param: string): string {
    // ...
};

How do I improve this so I don't see the tslint error?

我如何改进它,以便我看不到 tslint 错误?

回答by Kuba Jagoda

You have to explicitely add a type declaration to your variable.

您必须明确地向变量添加类型声明。

export var getCode : (param: string) => string = function (param: string): string { 
    //...
}

You said this looks pretty unreadable. Well, yes, anonymous types makes TS code look worse, especially when they are huge. In that case, you can declare a callable interface, like this:

你说这看起来很不可读。嗯,是的,匿名类型让 TS 代码看起来更糟,尤其是当它们很大时。在这种情况下,您可以声明一个可调用接口,如下所示:

export interface CodeGetter {
    (param: string): string;
}

export var getCode: CodeGetter = function(param: string): string { ... }

You can check whether tslint allows you (I can't check it right now) to drop type declaration in definition of the function when using the interface

您可以检查 tslint 是否允许您(我现在无法检查)在使用接口时删除函数定义中的类型声明

export interface CodeGetter {
    (param: string): string;
}

export var getCode: CodeGetter = function(param) { ... }

回答by donnut

Your code fragment looks fine. If this function is made to return a string, it compiles in tsc without an error. Are you sure the return value is a string?

您的代码片段看起来不错。如果此函数返回一个字符串,它会在 tsc 中编译而不会出错。你确定返回值是一个字符串?

This excerpt is from the tslint repo:

此摘录来自 tslint 存储库:

typedef enforces type definitions to exist. Rule options:

"callSignature" checks return type of functions
"indexSignature" checks index type specifier of indexers
"parameter" checks type specifier of parameters
"propertySignature" checks return types of interface properties
"variableDeclarator" checks variable declarations
"memberVariableDeclarator" checks member variable declarations

typedef 强制存在类型定义。规则选项:

"callSignature" checks return type of functions
"indexSignature" checks index type specifier of indexers
"parameter" checks type specifier of parameters
"propertySignature" checks return types of interface properties
"variableDeclarator" checks variable declarations
"memberVariableDeclarator" checks member variable declarations

回答by dug

Add a typedef to getCode:

给 getCode 添加一个 typedef:

var getCode: (s: string) => string;

Inline, it should look like this:

内联,它应该是这样的:

export var getCode: (s: string) => string = function (param) {
    // ...
};