You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// when method1 fails -> calls fallback with the same arguments of method1functionfallback<Method1FnextendsFunction,FallbackFnextendsMethod1Fn>(props: {fallbackFn: FallbackFn}){returnfunction(target: any,propertyKey: string,descriptor: TypedPropertyDescriptor<Method1Fn>){// implement fallback logic...returndescriptor};}// Call decorator with @ -> no compilation error :(classMyClass1{// I expect a compilation error because FallbackFn does not extend Method1Fn
@fallback({fallbackFn: (props: {x: number}):number=>{return0}})// works! why??staticmethod1Class(pros: {}): number{return1}}// Call decorator directly -> compilation error :)typeFallbackFn=((prpos: {x: number})=>number)typeMethod1Fn=((prpos: {})=>number)declareconstfallbackFn:FallbackFndeclareconstmethod1Fn:Method1Fn// Compilation error -- AS expected!!fallback<Method1Fn,FallbackFn>({fallbackFn })(null,"1",{value:method1Fn})// ^^^^^^^^^^
π Actual behavior
The decorator have constriant: FallbackFn extends Method1Fn but it is not honored when calling a decorator with @. But it is honored when calling the decorator as a function directly.
π Expected behavior
Expect the same error when calling the decorator with @ as we see when calling the decorator as a function directly.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered:
stavalfi
changed the title
Decorators genereics lose type safely with function as argument
Calling decorators with @ loses type safety with function as argument
Apr 29, 2024
This isn't a TypeScript bug and doesn't have anything to do with decorators, really. It's method parameter bivariance, which is working as intended. With --strictFunctionTypes enabled, Method1Fn and typeof MyClass1["method1Class"] are checked differently even though their IntelliSense looks the same. If you change Method1Fn to
π Search Terms
decorator, function, type-safety, @, covariance, contravariance
π Version & Regression Information
all versions
β― Playground Link
https://www.typescriptlang.org/play/?experimentalDecorators=true&emitDecoratorMetadata=true&ts=5.5.0-beta#code/PTAEHcAsFMDtQLbQC6QPYBMCMoBmBDASwBsBnUAWgD5QBjfYsvB4gI31oGsJDVRVooUviSh8AJwDmAVySxk5NLkQp02AFC5psWskJp4BRuy4AeALKrMWAGLxoAD2RwM5G9t37YAGlA2WJpx2oI7OsK6glqjWdlQAFAAO4mgJpABcoADezMYcQbAZ-rlcwQC+AJRZ6qCg4ijS4oYeegZxyBKSKBn4sACe3tU1oEkp0OLIvQDS0L0ZpMjihLCSA0OgGNCktIsJyGjiGQAqvQnQGAAKyafjvQAim9uEu-sWVtixg5WZgzUgoIQIBLEaByZA5Nh5UDENCSQi0AB0iJ+tXqjXWDx2e3Eg1KAG51KV1Oo-gBhFjo2j7fBYnh8AAClBosDQdDQgJI1K8IXEyXEoDScSJtGI+FI5HMvRJIrFOG+vzAAEkQg5TroxKz2SKWvYeftQKxoPRpKRBEUISV4Bg0JtQMywaEXJE3rZYIM6UZzZw4tkPYE7BlEld0llQA4MrBZAa+RU0hGEFHQABeGjZOrIBrwAAMoFKOcqf3A+04pAAhBBIL0APyVwbzTm0FTRbBS0WkQNoYPZGO2yNjKprNMZ0BYHEEomk8kbSnial6jCEOq6Yi9RkahIc7Xc3n88pEianPwBPLBROgOKBhIdjLZMM9+N9ipJpm98SVdT7wRRNQupNni9XkNH2TO8ozfdQpxFOpWVgeZwT9AozXg8DDUgwRKRgsEkCbF00i-GJXWJMASTZdctS5MZtwoChQAAQQAZWVVVnAwEsS00I8zDw94fEQ49YHiTJfT4vM4gjRhvAAIiwCTvEyAA3BhpGgNIsO-OwKkItYtO0moAD19IMwygA
π» Code
π Actual behavior
The decorator have constriant:
FallbackFn extends Method1Fn
but it is not honored when calling a decorator with@
. But it is honored when calling the decorator as a function directly.π Expected behavior
Expect the same error when calling the decorator with
@
as we see when calling the decorator as a function directly.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: