Skip to content

Extending Array in @typedef don't work as expected #42705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
NemoStein opened this issue Feb 9, 2021 · 1 comment
Closed

Extending Array in @typedef don't work as expected #42705

NemoStein opened this issue Feb 9, 2021 · 1 comment
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status. Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@NemoStein
Copy link

NemoStein commented Feb 9, 2021

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

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Feb 12, 2021
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.3.0 milestone Feb 12, 2021
@sandersn sandersn added the Working as Intended The behavior described is the intended behavior; this is not a bug label Apr 6, 2021
@sandersn
Copy link
Member

sandersn commented Apr 6, 2021

This is a special case of #42877.

@NemoStein I am curious what tool you use that understands this syntax, because I found little usage in the example code bases we use. Can you comment with that information on #42877?

@sandersn sandersn closed this as completed Apr 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status. Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants