Skip to content

Improved parameter names for call signatures resulting from unions #32056

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7117,9 +7117,11 @@ namespace ts {
}

function combineUnionParameters(left: Signature, right: Signature) {
const longest = getParameterCount(left) >= getParameterCount(right) ? left : right;
const leftCount = getParameterCount(left);
const rightCount = getParameterCount(right);
const longest = leftCount >= rightCount ? left : right;
const shorter = longest === left ? right : left;
const longestCount = getParameterCount(longest);
const longestCount = longest === left ? leftCount : rightCount;
const eitherHasEffectiveRest = (hasEffectiveRestParameter(left) || hasEffectiveRestParameter(right));
const needsExtraRestElement = eitherHasEffectiveRest && !hasEffectiveRestParameter(longest);
const params = new Array<Symbol>(longestCount + (needsExtraRestElement ? 1 : 0));
Expand All @@ -7129,11 +7131,16 @@ namespace ts {
const unionParamType = getIntersectionType([longestParamType, shorterParamType]);
const isRestParam = eitherHasEffectiveRest && !needsExtraRestElement && i === (longestCount - 1);
const isOptional = i >= getMinArgumentCount(longest) && i >= getMinArgumentCount(shorter);
const leftName = getParameterNameAtPosition(left, i);
const rightName = getParameterNameAtPosition(right, i);
const leftName = i >= leftCount ? undefined : getParameterNameAtPosition(left, i);
const rightName = i >= rightCount ? undefined : getParameterNameAtPosition(right, i);

const paramName = leftName === rightName ? leftName :
!leftName ? rightName :
!rightName ? leftName :
undefined;
const paramSymbol = createSymbol(
SymbolFlags.FunctionScopedVariable | (isOptional && !isRestParam ? SymbolFlags.Optional : 0),
leftName === rightName ? leftName : `arg${i}` as __String
paramName || `arg${i}` as __String
);
paramSymbol.type = isRestParam ? createArrayType(unionParamType) : unionParamType;
params[i] = paramSymbol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
//// ;
////
////callableThing4(/*4*/);
////
////declare const callableThing5:
//// | (<U>(a1: U) => void)
//// | (() => void)
//// ;
////
////callableThing5(/*5*/1)
////

verify.signatureHelp({
marker: "1",
Expand All @@ -50,4 +58,8 @@ verify.signatureHelp({
{
marker: "4",
text: "callableThing4(arg0: { x: number; } & { y: number; } & { z: number; } & { u: number; } & { v: number; } & { w: number; }): void"
},
{
marker: "5",
text: "callableThing5(a1: number): void"
});