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

Fix convert to named parameters rest parameter tuple #30286

Merged
merged 7 commits into from
Mar 18, 2019

Conversation

gabritto
Copy link
Member

@gabritto gabritto commented Mar 9, 2019

Fixes #30264.

This PR also restricts the availability of the named parameters refactor. The refactor will only be offered in a function with a rest parameter if the type of the rest parameter is either an array type or a tuple type. This is necessary because if a rest parameter is an array, the refactor needs to initialize the corresponding variable to preserve semantics:

function fn(a: boolean, ...b: number[]) {}
fn(true);

becomes:

function fn({ a, b = [] }: { a: boolean; b?: number[]; }) {}
fn({ a: true }); // b is not undefined in this call

If the rest parameter has a tuple type, then the parameter is not optional and providing an initializer is not necessary.
But in other cases it might not be possible to determine if the empty array initializer should be added (and adding the empty array as initializer might not even type check):

function /*a*/gen<T extends any[]>/*b*/(a: boolean, ...b: T): T {
    return b;
}

@gabritto gabritto requested a review from sandersn March 9, 2019 01:15
function isOptionalParameter(parameterDeclaration: ValidParameterDeclaration): boolean {
if (isRestParameter(parameterDeclaration)) {
const type = checker.getTypeAtLocation(parameterDeclaration);
return checker.isTupleType(type) ? false : true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return !checker.isTupleType(type)

@gabritto gabritto merged commit 10b9051 into master Mar 18, 2019
@gabritto gabritto deleted the fixConvertToNamedParametersRestParameterTuple branch March 18, 2019 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants