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
Presently generics in Typescript only mean something to the typescript compiler and the generated code isn't able to take advantage of them. I realize why this is, but I think there is a missed opportunity here. It would be nice if (where possible!) an actual data member variable were created according to the generic variable name. ie:
functiongeneric_add<T>(a: T,b:T): T{returna+b;}
would become:
functiongeneric_add_T(T,a,b){returna+b;}functiongeneric_add(a,b){constT=a.__proto__.constructor;// in current chrome this works for function and class types.generic_add_T(T,a,b);}
In the case that an Interface is passed as an argument (which has no existing run-time value) an argument should not be created. To help enforce this, a slightly different syntax:
interfaceIUsefulMethods{};// this can only be called where T is a type with a static implementation// of IUsefulMethods, which would imply that T is a type with a run-time value// so we would pass T as a hidden(to typescript) parameterfunctiongeneric_add<T: IUsefulMethods>(a: T,b:T): T{returna+b;}// T has no restrictions or declared interfaces// no guarantee that it has a run-time value// so we don't pass T as a hidden parameter.functiongeneric_sub<T>(a: T,b:T) : T{}
Use Cases
This would allow us to make generics actually useful in typescript beyond type-checking. If we can specify that a template argument extends from another type, then we should be able to access that type's static members.
I'm not sure what "based on the types of the expressions" means but this new syntax wouldn't introduce or attempt to introduce new JS syntax or type specialization code (as in emitted code that behaves differently depending on the type that is passed in).
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript / JavaScript code
This isn't a runtime feature (e.g. new expression-level syntax)
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
The text was updated successfully, but these errors were encountered:
Thinking about it and this is viable for projects that execute all of their own generic code but if javascript tries to make use of output generic code it would need knowledge of the generics. If we try to infer the type from .__proto__.constructor, then there's the possibility of the generic instance being null which is not ideal and in the case where there are no generic instances not possible at all and instead typescript would need to use an actual parameter and pass in the values for the parameter automatically. I think the solution where typescript automatically creates hidden parameters and fills them out according to the function call is ideal and then just expect users to read typescript documentation on a typescript project but this is one issue I see being troublesome.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Search Terms
Suggestion
Presently generics in Typescript only mean something to the typescript compiler and the generated code isn't able to take advantage of them. I realize why this is, but I think there is a missed opportunity here. It would be nice if (where possible!) an actual data member variable were created according to the generic variable name. ie:
would become:
In the case that an Interface is passed as an argument (which has no existing run-time value) an argument should not be created. To help enforce this, a slightly different syntax:
Use Cases
This would allow us to make generics actually useful in typescript beyond type-checking. If we can specify that a template argument extends from another type, then we should be able to access that type's static members.
Examples
This script:
Would be more readable like this:
The usefulness is more obvious when using this code:
Current:
Proposed:
Checklist
I'm not sure what "based on the types of the expressions" means but this new syntax wouldn't introduce or attempt to introduce new JS syntax or type specialization code (as in emitted code that behaves differently depending on the type that is passed in).
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: