-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Support defining lengths of arrays in function parameters with mapped tuples #28227
Comments
Duplicate of #26223? |
I don't think so. I don't want to specify the length of the tuple, I want to support any length. I just want the length of the 2 arrays to be the same. |
If you contextually type your tuple then you get some of the errors you want: const myStringArray: [string, string, string] = ["one", "two", "three"];
type MapToBoolean<T extends string[]> = { [K in keyof T]: boolean };
function mapStrings<T extends string[]>(strings: T, modifyStrings: MapToBoolean<T>) {
}
mapStrings(myStringArray, []); // should fail compilation - Does
mapStrings(myStringArray, [true, true]); // should fail compilation - Does
mapStrings(myStringArray, [true, true, true]); // should pass compilation - Does
mapStrings(myStringArray, [true, true, true, true]); // should pass compilation - Does NOT. 4 is not assignable to 3 I'm not sure whether the last one should be ok. I think the error seems reasonable. |
@jack-williams - that would be fine if I could require a tuple type as the function argument and not accept arrays. |
type VerifyTuple<T> =
T extends { length: infer L } ?
number extends L ? 'not a tuple' : T
: 'not an array';
const myStringArray = ["one", "two", "three"];
type MapToBoolean<T extends string[]> = { [K in keyof T]: boolean };
function mapStrings<T extends string[]>(strings: T & VerifyTuple<T>, modifyStrings: MapToBoolean<T>) {
}
mapStrings(myStringArray, []); // should fail compilation
mapStrings(myStringArray, [true, true]); // should fail compilation
mapStrings(myStringArray, [true,true, true]); // should pass compilation
mapStrings(myStringArray, [true, true, true, true]); // should pass compilation |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow. |
Search Terms
mapped tuple
Suggestion
Support the use of mapped tuples to define the length of arrays passed to a function
Use Cases
The actual example that I working with currently is that I want to construct a list of items, I pass an array of constructors to a function and I want to also pass to the function a list of configuration objects for each constructor. I tried to do this with a mapped tuple but the constructor list is typed as an array rather than a tuple so the length is not honoured and configuration lists of any length are allowed by the compiler
Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: