microsoft / microsoft/TypeScript
Allow returning types from functions
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Search Terms
Not easily googleable because all the words are the same for extracting the function's return type.
export type from function, return type from function
Suggestion
Make it possible to return a type from a function.
The type doesn't need to depend on the runtime values passed to the function, only on their types. This feature is just syntactic sugar.
Desired:
const {decorated_fn, DecoratedFnParams} = decorate(fn);
declare const decorated_fn_params: DecoratedFnParams;
decorated_fn(decorated_fn_params);
Workaround:
const decorated_fn = decorate(fn);
type DecoratedFnParams = DecoratedParams<typeof fn>;
declare const decorated_fn_params: DecoratedFnParams;
decorated_fn(decorated_fn_params);
Alternative syntax, since this only seems useful for functions that run once:
import {decorated_fn, DecoratedFnParams} = decorate(fn);
(Shouldn't be a compatibility breaking change for the same reasons import ... = require() isn't).
Use Cases
My case is decorating a class with multiple generic parameters. Currently I have to do this:
interface Bar extends BaseBar {}
interface Baz extends BaseBaz {}
class Foo<Bar, Baz> extends BaseFoo {}
const DecoratedFoo = decorator(Foo);
type DecoratedFooBar = ExtractBar<DecoratedFoo>;
type DecoratedFooBaz = ExtractBaz<DecoratedFoo>;
But it could be just const {DecoratedFoo, DecoratedFooBar, DecoratedFooBaz} = decorator(Foo);
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.
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
No source file, test, or entry point is named. Start by reviewing the requested destructuring syntax, the workaround, and the TypeScript Design Goals linked in the issue; done requires an agreed type-system design and corresponding behavior for the examples.
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
- 25/100