microsoft / microsoft/TypeScript
Feature request: Override and extend callback argument
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔍 Search Terms
extend callback argument typescript
✅ Viability Checklist
- 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, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
(Possibly a duplicate, but I couldn't find similar issue)
I often need to change type of a callback argument and it always takes an extra line to perform as assertions. I propose to add ability to use as keyword directly on the argument.
function withCallback(callback: (arg: { foo: string }) => void) {
// ...
}
withCallback((arg) => {
const { foo, bar } = arg as { foo: string; bar: string }; // always an extra line
// ...
});
📃 Motivating Example
It would be nice to have a built-in TS feature that makes possible to do that in a shorter manner:
withCallback(({ foo, bar } as { foo: string; bar: string }) => {
console.log(foo, bar);
});
// or
withCallback(({ foo, bar } as unknown as { foo: string; bar: string }) => {
console.log(foo, bar);
});
Overriding the type completely isn't needed too often that's why I propose to also enrich this syntax with extends (or something more applicable) keyword:
withCallback(({ foo, bar } as extends { bar: string }) => {
console.log(foo, bar);
});
Thinking thru it I think it would make sense to also add as extends for other often appearing use-cases:
const x = { foo: 'foo' };
const y = x as extends { bar: string };
// same as
const y = x as typeof x & { bar: string };
as extends T at this syntax can be interpreted as as typeof __SELF__ & T
💻 Use Cases
There are million use-cases for this feature. Type assertions in TS are used more often than we might expect while learning it.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names no source files, tests, or entry points. Start by comparing the proposed callback-parameter and as extends syntax forms and reviewing the design discussion; the work is complete only after one scoped proposal is agreed.
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
- Needs clarification
- Newbie friendliness
- 25/100