Skip to content

TS4055 when generating declaration for return type that uses type of paramter for protected methods #61591

New issue

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

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

Already on GitHub? # to your account

Open
zhanghai opened this issue Apr 18, 2025 · 2 comments
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@zhanghai
Copy link

zhanghai commented Apr 18, 2025

πŸ”Ž Search Terms

TS4055 declaration protected return typeof parameter

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about declarations

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.3#code/KYDwDg9gTgLgBASwHY2FAZgQwMbDgBSgjDRgWAGc4BvAKDjjCJNgE8BBALjiQFcBbAEZoA3PUbNSrAELcKMKMgDmYgL61aoSLDjpeSbGQhI4S4DELEpANUwAbXsAD6AeQDWACnFMrschW5LFjJKABpvSTYAOUx+YG43YFYIdAJIkIpwgEpA9P8AbRhWEhSJXyKYuIBdGnEoc14oEx9ggpapSuAqtQ0taHhsO0wKKnZahjBeQTsEbFNzIJt7R1dPcQm8ylzy-3CGDfLWToSk0sW-MPEctJ3KQuLgUvbo2K7x-fqYRubNinzniqvbridTiAD0YLgaCIUDgABUAMoAFgADABWNHcABKDSacCKJDgTymMzmcRgAAsIAATXREfhQ8D9YC0wbDKgU4ZE2EIKi8CjKCQIABumFQPFecAA5ACjq8pQA6CIQVCGFnzCzpVi2BzOACiUBhXn2ZVaWxuZsy61NHVeJ2SqXOGT2cGuToKBMeqVlnRqdBNn2+Noufx9QLEDFBDAhUMN0HhyJRAHYAMyBTBQV6oWEyrU6xxSonekmzODkqm09D0xnaVCsoYjOCcqjx3lwfmCpgisV4JCS3OHTqK5WqutwCgLPPLfVxqDG-YA-zbS0ug7BOVxe1nX6r4NFfPxC2kD0PJ5a30urLvaOQuEuAAiLhBtHUQA

πŸ’» Code

export interface Properties {
  propertyA: number;
  propertyB: string;
}

export function getPropertyValue_Ok(
  properties: Properties,
  propertyName: keyof Properties,
): Properties[typeof propertyName] {
  return properties[propertyName];
}

export class A {
  public getPropertyValue_Ok(
    properties: Properties,
    propertyName: keyof Properties,
  ): Properties[typeof propertyName] {
    return properties[propertyName];
  }

  // error TS4055: Return type of public method from exported class has or is using private name 'propertyName'.
  protected getPropertyValue_Error(
    properties: Properties,
    propertyName: keyof Properties,
  ): Properties[typeof propertyName] {
    return properties[propertyName];
  }

  // error TS4073: Parameter 'propertyValue' of public method from exported class has or is using private name 'propertyName'.
  protected setPropertyValue_Error(
    properties: Properties,
    propertyName: keyof Properties,
    propertyValue: Properties[typeof propertyName],
  ) {
    // TODO
  }
}

πŸ™ Actual behavior

The protected methods fail to generate declaration with TS4055 or TS4073.

πŸ™‚ Expected behavior

The protected methods should be able to generate declaration similar to the public method or function.

Additional information about the issue

I wonder if there's a way to temporarily suppress this error, becuase ts-ignore doesn't seem to work when generating declaration.

@RyanCavanaugh
Copy link
Member

This is a bug but there's also no reason to write this code in the first place. Doing typeof propertyName doesn't create a generic link between the call and the return type; it's identical to if you had written

  protected getPropertyValue_Error(properties: Properties, propertyName: keyof Properties): Properties[keyof Properties] {

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Apr 18, 2025
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Apr 18, 2025
@zhanghai
Copy link
Author

This is a bug but there's also no reason to write this code in the first place. Doing typeof propertyName doesn't create a generic link between the call and the return type; it's identical to if you had written

You are absolutely right. The reason I did it was mostly that when my actual interface name for Properties is much longer, writing typeof propertyName makes it shorter.

zhanghai added a commit to zhanghai/vnmark-player that referenced this issue Apr 18, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

2 participants