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

Use alias symbol for optional parameter declaration serialization #42170

Closed
wants to merge 2 commits into from

Conversation

andrewbranch
Copy link
Member

Fixes #42165

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Dec 31, 2020
@@ -5823,6 +5823,20 @@ namespace ts {
return !(getObjectFlags(type) & ObjectFlags.Reference) || !isTypeReferenceNode(existing) || length(existing.typeArguments) >= getMinTypeArgumentCount((type as TypeReference).target.typeParameters);
}

function serializeParameterTypeForDeclaration(context: NodeBuilderContext, type: Type, symbol: Symbol, enclosingDeclaration: Node | undefined, includePrivateSymbol?: (s: Symbol) => void, bundled?: boolean) {
if (type !== errorType && symbol.valueDeclaration?.kind === SyntaxKind.Parameter && (symbol.valueDeclaration as ParameterDeclaration).questionToken) {
Copy link
Member

@weswigham weswigham Jan 1, 2021

Choose a reason for hiding this comment

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

Do we have to look at the value declaration here? Or is there a (derived) CheckFlag on the symbol which indicates optionality? Moreover, do we already have an isOptionalParameterSymbol helper? I can't remember offhand.

@DanielRosenwasser
Copy link
Member

#42149 seems potentially related here.

context.approximateLength += 12; // " | undefined"
return factory.createUnionTypeNode([
typeToTypeNodeHelper(nonOptionalType, context),
factory.createKeywordTypeNode(SyntaxKind.UndefinedKeyword)

Choose a reason for hiding this comment

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

Do we need to union with undefined? I thought the nullable ? already implies that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Declaration emit (or any kind of type printback) always expands optional parameters/properties to their union type annotation, so this is just to match the convention. In some tangent on some other PR I floated the idea of reversing that decision for printback in errors and quick info to make it more succinct, but Anders wasn't keen on it.

At any rate, I'm pretty sure this PR is redundant with #42149. Now that it’s merged, have you tried your test case with it?

Copy link
Member

Choose a reason for hiding this comment

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

Hm - I have always kind of hated the extra | undefined unless we've lost the context where it came from.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it’s something we should revisit sometime.

@andrewbranch
Copy link
Member Author

Superseded by #42149

@andrewbranch andrewbranch deleted the bug/42165 branch January 12, 2021 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Declaration emit redundantly inlines optional union parameters
5 participants