Skip to content

Referencing a JSDoc @interface declaration in @param declaration produces an unexpected error flag (JavaScript with TS checks) #55652

Closed
@spillz

Description

@spillz

Does this issue occur when all extensions are disabled?: Yes

Steps to Reproduce:

Here's some sample JavaScript code

//@ts-check

/**
 * @interface Person
 * @property {string} name - The person's name.
 * @property {number} age - The person's age.
 */

/**
 * Greets a person.
 * 
 * @param {Person} person - The person to greet. //VS Code flags this as an error "Cannot find name 'Person'"
 * @returns {string} A greeting message.
 */
function greet(person) {
  return `Hello, ${person.name}! You are ${person.age} years old.`;
}

//Should be a valid call even though occupation is not part of the interface
greet({name:'Joe', age:25, occupation:'miner'});
//Should be an invalid call because age value is wrong type
greet({name:'Joe', age:'X', occupation:'miner'});

I would expect VS Code would flag the second invocation of greet as an error. Instead VS code flags the @param declaration as an error and allows both invocations of greet. Is this expected behavior or an upstream issue? From my reading of various JSDoc discussions, it seems that @interface isn't well supported across the entire ecosystem.

Failing that is the only way to achieve something like this where I can add arbitrary properties to an object as a function parameter that expects the object to have certain properties to use a .d.ts file with the interface definition and include that definition into my JSDoc?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions