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

chore(types): intellisense for colors #22151

Closed
wants to merge 1 commit into from
Closed

chore(types): intellisense for colors #22151

wants to merge 1 commit into from

Conversation

simonhaenisch
Copy link
Contributor

@simonhaenisch simonhaenisch commented Sep 23, 2020

Pull request checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)
  • Build (npm run build) was run locally and any changes were pushed
  • Lint (npm run lint) has passed locally and any fixes were made for failures

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe): Type Declarations

What is the current behavior?

The Color type is currently PredefinedColors | string with PredefinedColors being a string literal of the available predefined color names. However string is a super-type of any string literal and therefore this is reduced by TypeScript to just the string type (which is correct behavior). However it has the problem that no suggestions are made in Intellisense.

Issue Number: N/A

What is the new behavior?

See https://stackoverflow.com/a/61048124/2897426. There's a work-around that allows to have the correct type and also provide Intellisense for the string literal.

Does this introduce a breaking change?

  • Yes
  • No

Other information

One problem: any readme of a component that uses a prop with type Color would have this change:

+ `"danger" \| "dark" \| "light" \| "medium" \| "primary" \| "secondary" \| "success" \| "tertiary" \| "warning" \| string & {} \| undefined`
- `string \| undefined`
  • Good: No need to list the "default options" manually in the prop description anymore.
  • Bad: string & {} as a type is confusing.

(I have not pushed those readme changes yet.)

Maybe there's a way to get the readme generator to pick up Color as a type name instead of expanding the type? Or maybe we can just make it replace string & {} with string? (I could probably make a PR for Stencil).

@ionitron-bot ionitron-bot bot added the package: core @ionic/core package label Sep 23, 2020
@brandyscarney
Copy link
Member

Thanks for this! I think having it just replace string & {} with string would make sense. @adamdbradley ?

@sean-perkins
Copy link
Contributor

Thanks for this PR! After reviewing the changes and discussing with the team, we have decided not to move forward with these changes.

I believe an adaptation similar to:

type LiteralUnion<T extends U, U = string> = T | (U & Record<never, never>);

export type Color = LiteralUnion<PredefinedColors, string>;

will correct the intellisense behavior without regressing the build output for the docs. We will create a ticket internally to track this change and validate that it improves the intellisense behavior.

Thanks for doing the initial work and discovery here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: core @ionic/core package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants