我通过显式设置属性来为我的对象设置全局命名空间window.
window.MyNamespace = window.MyNamespace || {};
Run Code Online (Sandbox Code Playgroud)
TypeScript强调MyNamespace并抱怨:
属性'MyNamespace'在'window'类型的值上不存在任何"
我可以通过声明MyNamespace为环境变量并删除window显式来使代码工作,但我不想这样做.
declare var MyNamespace: any;
MyNamespace = MyNamespace || {};
Run Code Online (Sandbox Code Playgroud)
我怎样才能window留在那里让TypeScript开心?
作为旁注,我发现TypeScript抱怨特别有趣,因为它告诉我这种window类型any绝对可以包含任何东西.
How to do an array check (like Array.isArray()) with a readonly array (ReadonlyArray)?
As an example:
type ReadonlyArrayTest = ReadonlyArray<string> | string | undefined;
let readonlyArrayTest: ReadonlyArrayTest;
if (readonlyArrayTest && !Array.isArray(readonlyArrayTest)) {
// Here I expect `readonlyArrayTest` to be a string
// but the TypeScript compiler thinks it's following:
// let readonlyArrayTest: string | readonly string[]
}
Run Code Online (Sandbox Code Playgroud)
With an usual array the TypeScript compiler correctly recognises that it must be a string inside the if condition.