Reputation: 61
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
declare module 'MyTest' {
interface MyTest_FN1 {
(a: [string, ...Array<string>]): string[]
}
interface TestFN {
(name: string, ...args: string[]): string[]
}
}
/// <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
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)
}
}
/// <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]
}
Upvotes: 1
Views: 816