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

Add heuristics to detect type aliases that has complex typeparameters #10901

Merged
merged 10 commits into from
May 19, 2021

Conversation

sosukesuzuki
Copy link
Member

Description

Fixes #10857

Checklist

  • I’ve added tests to confirm my change works.
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

typeParams.length > 1 &&
!!getLast(typeParams).default &&
typeParams.every((param) => !!param.constraint)
) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Right now we only have the condition to fix just #10857, but I think we can improve here based on feedback in the future.

Comment on lines 255 to 256
Boolean(getLast(typeParams).default) &&
typeParams.every((param) => Boolean(param.constraint))
Copy link
Member

@thorn0 thorn0 May 16, 2021

Choose a reason for hiding this comment

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

Why check .default only for the last type parameter?

Suggested change
Boolean(getLast(typeParams).default) &&
typeParams.every((param) => Boolean(param.constraint))
typeParams.some((param) => param.constraint || param.bound || param.default)

param.bound is for Flow (playground).

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 forgot about param.bound, thank you.

Why check .default only for the last type parameter?

Because the default value of type parameters can only appear on last param.

Copy link
Member

@thorn0 thorn0 May 17, 2021

Choose a reason for hiding this comment

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

Because the default value of type parameters can only appear on last param

It's not the case.

update: oh, sorry, you were right

Copy link
Member Author

Choose a reason for hiding this comment

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

Like this?

type Bar<T = string, S> = {}

Copy link
Member

Choose a reason for hiding this comment

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

You're right, TS doesn't accept that. I thought S in that situation would default to any, but it doesn't work that way.

Copy link
Member

Choose a reason for hiding this comment

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

Still, I think it's better to break the LHS even if at least one type parameter has a constraint or a default (some, not every).

Copy link
Member Author

@sosukesuzuki sosukesuzuki May 17, 2021

Choose a reason for hiding this comment

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

Okay, I have no strong opinion

@thorn0
Copy link
Member

thorn0 commented May 17, 2021

Please add a changelog entry

@@ -0,0 +1 @@
run_spec(__dirname, ["flow", "babel"]);
Copy link
Member

Choose a reason for hiding this comment

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

Let's run flow test with babel-flow instead of babel.

Copy link
Member Author

Choose a reason for hiding this comment

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

@fisker fisker merged commit 4b9e881 into prettier:main May 19, 2021
@sosukesuzuki sosukesuzuki deleted the fix-10857 branch May 19, 2021 01:33
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ugly formatting for complex type parameters
3 participants