Closed
Description
Bug Report
In a js file (checkJs: true
) with JSDoc, extending the Array<T>
type doesn't allow to add properties to the extended type.
Considering this:
/**
* @template T
* @typedef {Array<T>} ArrayExtA
* @property {T} prop
*/
// No error here, but should, since `string[]` don't have property `prop`
/** @type {ArrayExtA<string>} */
const extA = ['']
// Error: Property 'prop' does not exist on type 'ArrayExtA<string>'.
console.log(extA.prop)
Typescript understood that ArrayExtA<string>
is an extension of Array<string>
, allowing ['']
to be assigned to the typed const extA
, but failed to add the prop
property.
Using a Intersection Type works, though
/**
* @template T
* @typedef Trait
* @property {T} prop
*/
/**
* @template T
* @typedef {Array<T> & Trait<T>} ArrayExtB
*/
// Error: Type 'string[]' is not assignable to type 'ArrayExtB<string>'.
/** @type {ArrayExtB<string>} */
const extB = ['']
// No error here, as expected
console.log(extB.prop)
Failing to assign ['']
to the typed const extB
is expected, since string[]
lacks the prop
property of ArrayExtB
🔎 Search Terms
jsdoc, typedef, extend, array
🕗 Version & Regression Information
4.2.0-dev.20210207
🙁 Actual behavior
@typedef {A} B
fails to add properties to type B
if type A
is Array
🙂 Expected behavior
Expected type B
to be a type A
with additional properties