DEV Community

Bruce Axtens
Bruce Axtens

Posted on

Typescript oddities?

Today I learned (and it's only 10:47am UTC+8 so who knows what else I'll learn today) that Typescript's external.d.ts can be confusing and helpful at the same time.

In VSCode, writing for Lychen in V8, I can have

if (CSSettings.ContainsKey("/MSG")) {
  console.log(CSSettings("/MSG"));
}
Enter fullscreen mode Exit fullscreen mode

This checks if the CSSettings object, which comes in from the C# side and is declared as Dictionary, contains the key "/MSG", and if it does, logs to the console a get from the dictionary using that key.

I had been trying for some time to put a declaration in external.d.ts that would cover both situations: CSSettings having a parameter and CSSettings having a method.

This is what I came up with after getting some clues from StackOverflow

declare function CSSettings(s:string):any;

declare namespace CSSettings {
    function ContainsKey(s:string):boolean;
}
Enter fullscreen mode Exit fullscreen mode

It looks like a classic symbol duplication situation, right? But no, Typescript just takes it in its stride and VSCode drops the wiggly red lines under both situations.

Weird.

Top comments (0)