Skip to content

JSDoc template interference #16485

Closed
Closed
@dyst5422

Description

@dyst5422

TypeScript Version: 2.4

Code
Example 0
ERROR: error TS2536: Type '"prop"' cannot be used to index type 'T'.
ERROR: error TS2339: Property 'prop' does not exist on type 'T'.

/**
 * Example 0
 * @class GenericClass
 * @template T
 */
class GenericClass<T extends { prop: string }> { // Doesn't work
  public insert(arg: T): T['prop'] {
    return arg.prop;
  }
}

Example 1
Works fine

/**
 * Example 1
 * @class OtherGenericClass
 */
class OtherGenericClass<T extends { prop: string }> { // Works
  public insert(arg: T): T['prop'] {
    return arg.prop;
  }
}

Example 2
ERROR: error TS2536: Type '"prop"' cannot be used to index type 'T'.
ERROR: error TS2339: Property 'prop' does not exist on type 'T'.

/**
 * Example 2
 * @template T
 * @param {T} arg
 * @returns {T['prop']}
 */
function myFunc<T extends { prop: string }> (arg: T): T['prop'] { // Doesn't work
  return arg.prop;
}

Example 3
Works fine

/**
 * Example 3
 * @param {R} arg
 * @returns {R['prop']}
 */
function myOtherFunc<R extends { prop: string }> (arg: R): R['prop'] { // Works
  return arg.prop;
}

Expected behavior:
Template declaration in JSDoc comments should not supersede the in-code template declarations.

Actual behavior:
In example 0 and 2 above, the generic type T is not found to properly extend { prop: string } when the JSDoc comment exists presumably because the JSDoc comment supersedes the type defined inline. This should never be the case where a non-code element takes precedence over the code contents.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions