admin管理员组

文章数量:1122826

I am writing a typescript declaration file declarations.d.ts that describes some interfaces and a global object. The declarations will be use to assist JavaScript developers that are writing script for an embedded v8 engine that offers global objects like the one being described.

There is a global variable Global of interface type SomeGlobal, which in turn has a property someType of interface type SomeType, and finally SomeType has a callback property of type function like (other:OtherType) => void. Here is the entire file: declarations.d.ts

interface OtherType
{
    stringProperty: string;
}
interface SomeType
{
    callback: (other:OtherType) => void;
}
interface SomeGlobal
{
    someType: SomeType;
    getSomeType(): SomeType;
}

declare var Global: SomeGlobal;

and the jsconfig.json being used to ensure that code in main.js can see these types:

{
    "compilerOptions": {
        "target": "es6",
        "lib": ["es6"],
        "strict": true
    },
    "include": [
        "*.js",
        "declarations.d.ts"
    ]
}

From here on, working in main.js:

In VSCode when you attempt to assign the callback property of SomeType and open the parentheses to start building the arrow function like so:

Global.someType.callback = (

I would expect to see autocomplete help like callback(other: OtherType): void, prompting me to complete the statement like:

Global.someType.callback = (other) => {/* do something*/};

But this is not what happens. If I hover over callback VS will display the appropriate signature for the property (property) SomeType.callback: (other: OtherType) => void, but inside the arrow function param list or funtion body, the other param just shows up as type any.

What's interesting is that if I go through a function call instead of a property:

Global.getSomeType().callback = (

Then there is correct function type inference for callback and I get the intellisense autocomplete you'd expect.

Am I just missing something silly here?

I have tried searches of all kinds via Google and SO. I have tried various combinations of declaring the types as "type", "interface" or "class". I have tried declaring the types as classes and then just doing:

let testGlobal = new SomeGlobal();
// then
testGlobal.someType.callback = (other) => {};

still no dice, so it's not the way the global is declared.

本文标签: