TSC 后静态成员丢失
Static members going missing after TSC
我有一个 class 包含我在打字稿中声明为静态的全局值。
看起来像这样:
export default class Globals {
// All members should be public static - no instantiation required
public static GraphAPIToken: null
public static APP_ID: "appidstringhere"
public static APP_SECRET: "thisisasecret"
public static TOKEN_ENDPOINT: "https://login.microsoftonline.com/aaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeeeeeee/oauth2/v2.0/token"
public static MS_GRAPH_SCOPE: "https://graph.microsoft.com/.default"
}
使用 TSC (Typescript 3.7.3) 编译为 js 后,结果如下:
"use strict";
exports.__esModule = true;
var Globals = /** @class */ (function () {
function Globals() {
}
return Globals;
}());
exports["default"] = Globals;
我的问题是,我的会员怎么了!?
欢迎提出任何想法:)
您实际上并没有分配您的成员,而是将它们定义为具有类型的未定义变量。使用 =
而不是 :
.
export default class Globals {
// All members should be public static - no instantiation required
public static GraphAPIToken = null;
public static APP_ID = "appidstringhere";
public static APP_SECRET = "thisisasecret";
public static TOKEN_ENDPOINT = "https://login.microsoftonline.com/aaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeeeeeee/oauth2/v2.0/token";
public static MS_GRAPH_SCOPE = "https://graph.microsoft.com/.default";
}
附加上下文:TypeScript 允许字符串和整数常量作为类型工作,因此如果您传入字符串,接受 left
和 right
的参数将抛出编译时错误那不是这两个值之一。与对象定义不同,例如 {foo: "bar"}
,您定义了一个 class
,而 TypeScript 中的 class 字段使用 :
定义类型,使用 =
定义值.
我认为有趣的是 TypeScript 不会抱怨不应该接受 undefined
,但这是一个单独的问题。
在我看来,您在这里将静态变量的类型声明为静态变量的预期值。例如:
public static APP_ID: "appidstringhere"
表示 APP_ID
是 "appidstringhere"
类型,而您应该说:
public static APP_ID: string = "appidstringhere"
表示 APP_ID
的类型为 string
,并且值为 "appidstringhere"
.
我有一个 class 包含我在打字稿中声明为静态的全局值。
看起来像这样:
export default class Globals {
// All members should be public static - no instantiation required
public static GraphAPIToken: null
public static APP_ID: "appidstringhere"
public static APP_SECRET: "thisisasecret"
public static TOKEN_ENDPOINT: "https://login.microsoftonline.com/aaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeeeeeee/oauth2/v2.0/token"
public static MS_GRAPH_SCOPE: "https://graph.microsoft.com/.default"
}
使用 TSC (Typescript 3.7.3) 编译为 js 后,结果如下:
"use strict";
exports.__esModule = true;
var Globals = /** @class */ (function () {
function Globals() {
}
return Globals;
}());
exports["default"] = Globals;
我的问题是,我的会员怎么了!?
欢迎提出任何想法:)
您实际上并没有分配您的成员,而是将它们定义为具有类型的未定义变量。使用 =
而不是 :
.
export default class Globals {
// All members should be public static - no instantiation required
public static GraphAPIToken = null;
public static APP_ID = "appidstringhere";
public static APP_SECRET = "thisisasecret";
public static TOKEN_ENDPOINT = "https://login.microsoftonline.com/aaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeeeeeee/oauth2/v2.0/token";
public static MS_GRAPH_SCOPE = "https://graph.microsoft.com/.default";
}
附加上下文:TypeScript 允许字符串和整数常量作为类型工作,因此如果您传入字符串,接受 left
和 right
的参数将抛出编译时错误那不是这两个值之一。与对象定义不同,例如 {foo: "bar"}
,您定义了一个 class
,而 TypeScript 中的 class 字段使用 :
定义类型,使用 =
定义值.
我认为有趣的是 TypeScript 不会抱怨不应该接受 undefined
,但这是一个单独的问题。
在我看来,您在这里将静态变量的类型声明为静态变量的预期值。例如:
public static APP_ID: "appidstringhere"
表示 APP_ID
是 "appidstringhere"
类型,而您应该说:
public static APP_ID: string = "appidstringhere"
表示 APP_ID
的类型为 string
,并且值为 "appidstringhere"
.