-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Describe function signature using an interface #34319
Comments
This also allows developers to define interfaces to design functions, without implementing them (meaning no empty un-implemented function blocks). The developer could then use interfaces within tests or code before the implementation is available. Right now I am defining interfaces and functions separately (but with same constraints), letting me design the constraints and having one of our developers who is new to typescript (and javascript) fill in the implementation. The implementation is then passed to a test that accepts a function with the interface's signature.
Using this pattern also means the argument types or return type doesn't need to be defined, as the interface has already done this (this wasn't mentioned in the above issue, but is in the example) I do have a concern though that An example of type gymnastics being resolved: Function type interface: interface SearchFunc {
(source: string, subString: string): boolean;
} Current: let mySearch: SearchFunc;
mySearch = function(source: string, subString: string) {
let result = source.search(subString);
return result > -1;
} Proposed: function mySearch(source, subString) implements SearchFunc {
let result = source.search(subString);
return result > -1;
} Current Source: https://www.typescriptlang.org/docs/handbook/izterfaces.html#function-types |
This would also allow this pattern: interface Dispatcher<T> {
(value: T): boolean;
}
function dispatch<T>(value: T, fn?: Dispatcher<T>) implements Dispatcher<T> {
if (fn) {
return fn(value);
}
// Do something default
return false;
} The alternative would be duplicating the types: function dispatch<T>(value: T, fn?: Dispatcher<T>): boolean {
if (fn) {
return fn(value);
}
// Do something default
return false;
} It may not be significant, but it reduces the surface area of where types are defined (e.g. I can change the return type on the interface and it would also change the return type of the function), which in my opinion is a bonus. In the example I have just used This sneaked into this comment's examples as well, but any arguments not defined in the argument list of the interface would need to be optional, else the function couldn't match the defined interface, the alternative would be not to allow additional arguments, but this is avoidable |
Regardless of the syntax (which I think is quite OK), it solves the problem of not being able to make function declarations implement anything, so now I have to write One thing to bear in mind is that the type of the arguments can only be optional if the interface/interfaces declare one call signature. I think it's reasonable the function implements on or more interfaces without call signatures. With multiple call signatures, it becomes an overloaded function and should declare compatible arguments (which I believe is the current rule for implementing overloaded functions) . Another issue is the hoisting and TDZ for the properties/methods. It would be great if we could preserve the current behaviour. interface Something {
value: number;
}
something.value; // should be illegal
something.value = 42; // should be OK
something.value; // should be OK
function something() implements Something {} |
That is a great point, you aren't able to provide their complete type ahead of time using current syntax because you cannot define properties. TypeScript is then maybe breaking the goal of As with your example, it would work the same as let: let value: number;
console.log(value); // should be illegal
value = 42; // should be OK
console.log(value); // should be OK |
This could help me implement function property (like here https://www.bennadel.com/blog/3250-defining-functions-with-properties-using-typescript-declaration-merging-in-angular-2-4-9.htm) when using React.FunctionComponent. |
Did this functionality ever get added to typescript? Or is there some kind of work around? I couldn't tell if there was a conclusion that was reached on this topic. |
I came across this problem back a while ago and after a little research I found this thread. const MyComponent: FC<Props<T>> = <T>(props) => { ... } (in the above example, we don't have access to the function generic type at the point of declaring the MyComponent Type so the So we have to use the function form, but by doing that, we won't be able to use the function <T>MyComponent(props: Parameters<FC<Props<T>>>[0]): ReturnType<FC<Props<T>>> { ... } It isn't that beautiful, but it wouldn't matter if it worked. even with this syntax, we won't get proper type check for things like So after all, it would be great if typescript had first-class support for this kind of situation. I'd love to be able to do something like this: function <T>MyComponent(props) implements FC<Props<T>> {
...
} |
is there some kind of work around for until this feature gets implemented? |
@RyanCavanaugh any status update on this one? |
@RyanCavanaugh Can we get something like this? |
Hey folks, we've got a few thousand open suggestions to manage, and pings for "updates" are no-ops because we'll always post updates in-thread. Thanks! |
In addition to the above given syntax, if we completely forgot about the interface CoolPrototype {
foo(): boolean
}
interface CoolConstructor {
new (): CoolPrototype
(): CoolPrototype
}
function coolConstructor() implements CoolConstructor {
if (!(this instanceof coolConstructor)) return new coolConstructor()
return this
}
coolConstructor.prototype = {
foo() {
return true
}
} By dropping the TypeScript stuff, this works as I would expect, matching the types defined... I originally mentioned the property to be optional, but by having it required, the type checker would/could need to see if there is assignment to that property (what happens if I wanted to do Here is also a playground link with how you can achieve the same (constructor + normal function) with a type, with usage of This would also allow us to do to this... let cached: CoolPrototype
function coolConstructor() implements CoolConstructor {
if (cached) return cached
if (!(this instanceof coolConstructor)) return new coolConstructor(...arguments)
cached = this
return this
}
coolConstructor.prototype = {
foo() {
return true
}
}
console.log(coolConstructor() === coolConstructor())
console.log(coolConstructor() === new coolConstructor()) Of course we could just remember the |
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 useany
. Referencing: https://www.typescriptlang.org/docs/handbook/functions.html#overloadsIt would also allow optional properties to be defined on a function without doing type gymnastics.
Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: