microsoft / microsoft/TypeScript
Describe function signature using an interface
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Search Terms
function implements interface
Suggestion
Allow functions to indicate interfaces that they implement, allowing usage of a function interface.
Properties defined on an interface would not be possible unless they are defined as optional
Use Cases
When using function overloads, the return type is defined as any, this would allow the return type to be guarded and not use any. Referencing: https://www.typescriptlang.org/docs/handbook/functions.html#overloads
It would also allow optional properties to be defined on a function without doing type gymnastics.
Examples
interface CoolFunction {
(first: string, second: number): number;
(first: number, second: string): string;
property?: boolean;
}
// args will be of type [string, number] | [number, string]
function coolFunction(...args) implements CoolFunction {
if (typeof args[0] === "string") {
// We're now reduced our type of args to [string, number]
return args[1];
} else {
// args can only be [number, string]
return args[1];
}
}
// This property is known as it is defined on the interface
coolFunction.property = true;
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
Start by reviewing the proposed implements syntax and the linked TypeScript handbook section on function overloads. Define how overloads, optional interface properties, argument narrowing, and return types should behave, while preserving the stated no-runtime-change constraint; the issue does not name implementation files or 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
- 35/100