Skip to content

TS1092: Type parameters cannot appear on a constructor declaration #10860

Description

@yortus

TypeScript Version: nightly (2.1.0-dev.20160906)

Code

// (A) declare Wrapper class directly - THIS DOESN'T WORK// The errors below are all 'TS1092: Type parameters cannot appear on a constructor declaration'declareclassWrapper{constructor<TR>(wrapped: ()=>TR): NullaryWrapper<TR>;// ERRORconstructor<TR,T0>(wrapped: ($0: T0)=>TR): UnaryWrapper<TR,T0>;// ERRORconstructor<TR,T0,T1>(wrapped: ($0: T0,$1: T1)=>TR): BinaryWrapper<TR,T0,T1>;// ERRORinvoke(...args: any[]): any;}// (B) declare Wrapper as static side + instance side interfaces - THIS WORKS FINE// This is equivalent to the class declaration in (A), but in this case it works without errors.declareletWrapperConstructor: {new<TR>(wrapped: ()=>TR): NullaryWrapper<TR>;new<TR,T0>(wrapped: ($0: T0)=>TR): UnaryWrapper<TR,T0>;new<TR,T0,T1>(wrapped: ($0: T0,$1: T1)=>TR): BinaryWrapper<TR,T0,T1>;}// Some strongly-typed versions of the Wrapper shape for specific aritiesinterfaceNullaryWrapper<TR>extendsWrapper{invoke(): TR;}interfaceUnaryWrapper<TR,T0>extendsWrapper{invoke($0: T0): TR;}interfaceBinaryWrapper<TR,T0,T1>extendsWrapper{invoke($0: T0,$1: T1): TR;}// Some functions to be wrappedconstf0=()=>42;constf1=(s: string)=>42;constf2=(s: string,n: number)=>42;// Create some wrappers using the Wrapper class - DOESN'T WORKnewWrapper(f0).invoke;// ERRORnewWrapper(f1).invoke;// ERRORnewWrapper(f2).invoke;// ERROR// Create some wrappers using the WrapperConstructor var/interface - WORKS NICELYnewWrapperConstructor(f0).invoke;// () => numbernewWrapperConstructor(f1).invoke;// ($0: string) => numbernewWrapperConstructor(f2).invoke;// ($0: string, $1: number) => number

I'm trying to infer a strongly-typed shape for the class instance from the arguments passed to the constructor. There is no runtime shenanigans, this is purely for improved type-checking.

Are the TS1092 errors in the code at (A) above really necessary, given that the equivalent code at (B) works fine? I'd rather be able to write the Wrapper class directly than use the WrapperConstructor workaround of a var/interface combination.

They do appear to be equivalent typewise, but the (B) version seems a bit hacky. And if the compiler can accept (B), couldn't it also allow (A)?

Or is there a better way to do what I'm trying to do here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions