Skip to content
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

Closed
4 tasks done
Roaders opened this issue Oct 30, 2018 · 6 comments
Closed
4 tasks done
Labels
Question An issue which isn't directly actionable in code

Comments

@Roaders
Copy link

Roaders commented Oct 30, 2018

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

const myStringArray = ["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
mapStrings(myStringArray, [true, true]); // should fail compilation
mapStrings(myStringArray, [true, true, true]); // should pass compilation
mapStrings(myStringArray, [true, true, true, true]); // should pass compilation

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • 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
  • This isn't a runtime feature (e.g. new expression-level syntax)
@AlCalzone
Copy link
Contributor

Duplicate of #26223?

@Roaders
Copy link
Author

Roaders commented Oct 30, 2018

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.

@jack-williams
Copy link
Collaborator

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.

@Roaders
Copy link
Author

Roaders commented Oct 30, 2018

@jack-williams - that would be fine if I could require a tuple type as the function argument and not accept arrays.

@jack-williams
Copy link
Collaborator

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

@weswigham weswigham added the Question An issue which isn't directly actionable in code label Oct 30, 2018
@typescript-bot
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

5 participants