Skip to content
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

Allow decorated accessor properties to have a different type #60200

Open
6 tasks done
NullVoxPopuli opened this issue Oct 11, 2024 · 2 comments
Open
6 tasks done

Allow decorated accessor properties to have a different type #60200

NullVoxPopuli opened this issue Oct 11, 2024 · 2 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@NullVoxPopuli
Copy link

🔍 Search Terms

I was pretty sure an issue already existed for this, but I couldn't find it. Hopefully opening a new issue (to maybe be closed again) will improve SEO.

Here is what I searched for:

  • decorators change type
  • decorators accessor type
  • decorators change type accessor

I did find this related issue:

✅ Viability Checklist

⭐ Suggestion

Allow decorator's return type, ClassAccessorDecoratorResult to specify a different Value-type than what is on the right-hand side of the equals.

Here is how TS 5.6.3 handles this situation today.

📃 Motivating Example

For example, it should be possible to author this with 0 errors:

class Demo {
  @unwrap accessor num = { wrapped: 2 };
  //               ^? => number

  get usage() {
    expectTypeOf(this.num).toEqualTypeOf<number>();
  }
}

💻 Use Cases

  1. Reactivity
  2. Hiding complex behaviors from the user
  3. Unwrapping private objects into public API (the example in the playground link)
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Oct 16, 2024
@shigma
Copy link

shigma commented Oct 28, 2024

A related issue for method decorators: #49229

@lilnasy
Copy link

lilnasy commented Nov 28, 2024

I ran into this while attempting to type decorator example at proposal-signals/signal-polyfill/README.md#decorators.

relevant code excerpt
import { Signal } from "signal-polyfill";

export function signal(target) {
  const { get } = target;

  return {
    get() {
      return get.call(this).get();
    },

    set(value) {
      get.call(this).set(value);
    },

    init(value) {
      return new Signal.State(value);
    },
  };
}

The limitation seems to be that typescript assumes the publicly exposed value will have the same type as the value stored on the private accessor storage. The code example is a counter-example to that assumption: the type of the internal value is Signal.State<PublicValue> instead.

More specifically, the interfaces ClassAccessorDecoratorTarget, and ClassAccessorDecoratorResult are limited:

  • ClassAccessorDecoratorTarget needs to allow the second generic argument to be unrelated to the public value type. This is because it reads from and writes to the accessor storage, not the public instance.
  • ClassAccessorDecoratorResult needs to allow the return value type of .init() to be unrelated to the return value type of .get() and the paramter value type of .set(). This is because .init() relates to the value on the accessor storage, not the public value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants