Using @type for declare type of class method in JavaScript

When I use @type to declare type of class method in JavaScript with d.ts (see Type Checking JavaScript Files), the parameter types are determined incorrectly, if I use the short form of the method entry. If I use a form with function, everything works correctly.

It's bug, or implemented so intentionally?

PS: using an interface for a class with methods defined in it also does not work

common test.d.ts

declare module 'MyTest' {

  interface MyTest_FN1 {
    (a: [string, ...Array<string>]): string[]
  }

  interface TestFN {
    (name: string, ...args: string[]): string[]
  }
}

It doesn't work:

/// <reference path="./test.d.ts"/>
// @ts-check

/** test class */
class MyTest {

  /** @type {import('MyTest').MyTest_FN1} */
  fn1(args) {
    return testFn(...args)
  }

}

/** @type {import('MyTest').TestFN} */
function testFn(name, ...args) {
  return [name, ...args]
}
npx tsc --allowJs --checkJs --noEmit ./test.js
> test.js:9:19 - error TS2556: A spread argument must either have a tuple type or be passed 

enter image description here

If you add @implements, the problem remains:

  interface MyTestCLS {
   fn1: MyTest_FN1
  }
/** @typedef {import('MyTest').MyTestCLS} IMyTest */
/** @implements {IMyTest} */
class MyTest {

  /** @type {import('MyTest').MyTest_FN1} */
  fn1(args) {
    // args hav a type??
    return testFn(...args)
  }

}

enter image description here

It works:

/// <reference path="./test.d.ts"/>
// @ts-check

/** test class */
class MyTest {

  /** @type {import('MyTest').MyTest_FN1} */
  fn1 = function test(args) {
    // args have a type??
    return testFn(...args)
  }

}

/** @type {import('MyTest').TestFN} */
function testFn(name, ...args) {
  return [name, ...args]
}

enter image description here

Upvotes: 1

Views: 816

Answers (0)

Related Questions