-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Strict check arity of method that implements interface, type or class #46881
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
Comments
This is intentional and mentioned in the FAQ. You can use properties instead. |
Thank you for fast response . |
|
@MartinJohns |
I completely misread your issue, I'm sorry. I thought this was about implementations being able to accept more "wide" types than the interface (bivariance). But your issue is mentioned in the FAQ as well: Why do you believe this should be an error? If the implementation can perform the work with less parameters, then so be it. |
Thank you for link! Notice I did not make If type or interface define function Actually I found this issue while working on some project // old interface
//
export interface ITranslate {
isValidLocale(targetLocale: string): Promise<boolean>;
translateText(
text: string,
targetLocale: string
): Promise<string>;
}
//new interface
export interface ITranslate {
isValidLocale(targetLocale: string): Promise<boolean>;
translateText(
text: string,
sourceLocale: string, //add this because of aws
targetLocale: string
): Promise<string>;
}
// old google implementation compiles even I add sourceLocale
// google.ts
async translateText(
text: string,
targetLocale: string
): Promise<string>
// aws.ts
async translateText(
text: string,
sourceLocale: string,
targetLocale: string
): Promise<string> So you see in new interface I add parameter sourceLocale , because It would be nice that ts compiler warn me about this . In link that you provided they are speaking about callback |
Is there is some way to change issue type instead of Bug |
Yeah, it's not a bug. There's no practical difference between hi(a: string): void {
/* do stuff */
} and hi(a: string, b: string): void {
/* do the exact same stuff and completely ignore `b` */
} On the other hand, adding a third parameter This logic applies equally for both callbacks and interfaces. |
@fatcerberus agree it is not a bug |
Turning this feature on, you would quickly discover that you want to turn it off again. Ignoring trailing parameters is common and idiomatic JS that presents no runtime problems. |
Hi @RyanCavanaugh thank you for reply, but I do not agree. Personally I would always turn it on in my code base. I give real example where it could help catching runtime error during compile time: From my example above: add second parameter to method signature translateText above Not sure why you speak about runtime problems, maybe you did not understand my request !? I was more speaking about catching runtime errors during compile time, Even turning on feature, review code and turning off it again it is better then I think it could be nice to have this flag , but maybe it is complicated to implement it. Notice also that TypeScript has power to specify optional parameters ` interface I { Quite often I have similar discussion with my collogues: |
Even ChatGPT is confused by this one : think it should be error |
Bug Report
π Search Terms
is:issue is:open strict check interface implementation
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
class A compiles even hi method does not have 2 arguments
π Expected behavior
class A should not compiles
similar like class B maybe some new strict flag need to be added in case
strictCheckImplementationArity or something like this
It would be nice to catch this errors in compile time
otherwise in runtime arguments can be wrongly passed
The text was updated successfully, but these errors were encountered: