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

infer argument types for generic method decorator factory #39903

Closed
5 tasks done
rhyek opened this issue Aug 4, 2020 · 1 comment
Closed
5 tasks done

infer argument types for generic method decorator factory #39903

rhyek opened this issue Aug 4, 2020 · 1 comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@rhyek
Copy link

rhyek commented Aug 4, 2020

Search Terms

TypedPropertyDescriptor generic method decorator arguments

Suggestion

I would like for a method decorator factory to be able to receive a callback as an argument whose own arguments mirror the method the decorator is being applied to.

Use Cases

I would like to write a custom memoize decorator for methods that allows me to provide a function which would return a cache key based on the original method's arguments.

Examples

function cached<TargetType extends Object, ArgumentsType extends any[], ReturnType = any>(cacheKey: (...args: ArgumentsType) => string) {
  return function(
    target: any,
    key: PropertyKey,
    descriptor: TypedPropertyDescriptor<(...params: ArgumentsType) => Promise<ReturnType>>
  ) {
    descriptor.value = async function(this: TargetType, ...args: ArgumentsType) {
      const _cacheKey = cacheKey(...args);
      const result = getFromCache(_cacheKey) ?? descriptor.value.apply(this, ...args);
      return result;
    }
  }
}

class Example {
  @cached(param1 => param1.toUpperCase()) // should know param1 is of type string
  async work(param1: string) {
    return networkRequest(param1);
  }
}

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@rhyek rhyek changed the title infer argument types for generic method decorator infer argument types for generic method decorator factory Aug 4, 2020
@RyanCavanaugh RyanCavanaugh added Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript labels Sep 2, 2020
@RyanCavanaugh
Copy link
Member

We're not expanding the current experimental decorator implementation any further since this is something we're trying to standardize through the TC39 proposal. Suggestions for more behavior there should be taken to the tc39 proposal repo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants