-
Notifications
You must be signed in to change notification settings - Fork 38
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
generic rest parameters #101
Comments
Say {
0: Number,
1: String,
2: Boolean,
length: Number
} We can calculate this type using a JavaScript function function CollectionType(types) {
var collectionType = {};
for (var t in types) {
collectionType[t] = types[t];
}
return collectionType;
} and define interface Collection[...T] {
...(CollectionType(T)),
length: Number
} How is
|
Collection[Number, String, ...Boolean] is equal to {
...Dictionary[Number, Boolean],
0: Number,
1: String,
length: Number
} Note: See #99 for definition of Since interface Spread[T] {
type: T
} to describe Now we can define interface Collection[...T] {
...(RestElements(T))
...(CollectionType(T)),
length: Number
} where function CollectionType(types) {
var collectionType = {};
for (var t in types) {
if (!isSpread(types[t])) { // exclude spread type argument
collectionType[t] = types[t];
}
}
return collectionType;
}
function RestElements(types) {
if (types.length > 0) {
var lastType = types[types.length - 1];
if (isSpread(lastType)) {
return Dictionary(Number, lastType.type);
}
}
return {};
}
function isSpread(type) {
return type instanceof Spread;
} |
I like |
As mentioned in #96 we can describe generics that take an indefinite number of type arguments using rest parameter syntax:
But how can we access a specific type (argument) of the passed rest arguments?
The text was updated successfully, but these errors were encountered: