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
Some of these meta types can have corresponding meta operators: & can denote $Merge, + can denote $Concat and so on; while other meta types, like $Args, won't have a corresponding operator. Such operators will essentially allow to write expressions that operate on types and construct other types.
Lennart (@Lenne231), I'm not familiar with type providers, but a brief look at #3136 made me think that it's an imperative/procedural way of constructing types at compile time, while what I'm suggesting is a declarative way. If this is the case, then type providers is indeed a much more powerful feature.
It seems, that the appropriate place for such "generic meta types" (that would better be called "type expressions" or "computed types") is the .d.ts files. Currently they can contain only static type definitions, but could also contain "dynamic definitions":
Such computed types could be compiled in some special "compilation context" in which all these Type and ObjectLiteral are defined. Then we could go even further and allow "type operators":
It seems that part of the motivating case could be met by extending typeof to arbitrary expressions, as per #4233.
type C = $Merge<{a: number}, {b: string}>; // C = { a: number, b: string }
Would then become
// $Merge woiuld provide 20 or so overloads with 1 to 20 parameters.
// Here's the two parameter overload.
function $Merge<T1>(a1: T1): (T1&T2);
function $Merge<T1, T2>(a1: T1, a2: T2): (T1&T2);
function $Merge<T1, T2, T3>(a1: T1, a2: T2, a3: T3): (T1&T2&T3);
function $Merge(...args){return null;}
type C = typeof $Merge({a: number}, {b: string});
Activity
Lenne231 commentedon Jul 8, 2015
I like the idea, but maybe this can also be solved with type providers (#3136). This would be way more flexible.
danquirk commentedon Jul 8, 2015
It seems like $Merge would be better described through intersection types:
$Args and $RetV look like they're essentially compile time reflection APIs.
d180cf commentedon Jul 9, 2015
Some of these meta types can have corresponding meta operators:
&can denote$Merge,+can denote$Concatand so on; while other meta types, like$Args, won't have a corresponding operator. Such operators will essentially allow to write expressions that operate on types and construct other types.Lennart (@Lenne231), I'm not familiar with type providers, but a brief look at #3136 made me think that it's an imperative/procedural way of constructing types at compile time, while what I'm suggesting is a declarative way. If this is the case, then type providers is indeed a much more powerful feature.
d180cf commentedon Jul 23, 2015
Btw, does TS have plans to support nested types:
interface Foo { type Bar = number; }? If yes, then it seems natural to have built-in nested types:danquirk commentedon Jul 23, 2015
d180cf local/nested types are already in, planned for 1.6: #3266
Lenne231 commentedon Jul 27, 2015
A "generic meta type" that creates a subset would also be nice, especially for functions that update data.
d180cf commentedon Jul 28, 2015
It seems, that the appropriate place for such "generic meta types" (that would better be called "type expressions" or "computed types") is the .d.ts files. Currently they can contain only static type definitions, but could also contain "dynamic definitions":
Such computed types could be compiled in some special "compilation context" in which all these
TypeandObjectLiteralare defined. Then we could go even further and allow "type operators":Of course, some of these computed types will be pretty common and will naturally become a part of the standard types library:
This should make it possible to express all fancy constructs that can exist in JS.
benliddicott commentedon Sep 14, 2015
It seems that part of the motivating case could be met by extending
typeofto arbitrary expressions, as per #4233.Would then become
Args is harder to see.
mhegazy commentedon Feb 22, 2016
this is already covered by #2710