microsoft / microsoft/TypeScript

Make ParameterDecorator and the like generic

Open
#42,528 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

lib Update Request

Missing / Incorrect Definition

ParameterDecorator is currently not generic, it can be attachted to any parameter. However, when a decorator is used for e.g. Dependency Injection purposes, some decorators require the type of the parameter to be consistent with the type of the decorator

Sample Code

E.g. we want this to work

class MyController {
  requestThings(@UserId() userId: number): Thing[] { return [] }
}

But this to fail:

class MyController {
  requestThings(@UserId() userId: string): Thing[] { return [] }
}

(as userId will be a number, not a string so the injection should ideally not be possible (at compile time)

Currently UserId is done more or less this way:

const UserId = (): ParameterDecorator => (target, key, index) => { 
  // do stuff
}

while I'd like to add number to ParameterDecorator:

const UserId = (): ParameterDecorator<number> => (target, key, index) => { 
  // do stuff
}

and I'd like typescript to check this when applying a decorator.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the ParameterDecorator definition and the compiler checks for applying parameter decorators. Compare the issue's number and string examples to the current behavior, then determine how generic parameter types should be represented and validated. Done means valid decorator applications are accepted and mismatched parameter types are rejected with appropriate tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.