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

Typescript typeof invalid formatting #14283

Closed
dfadev opened this issue Feb 1, 2023 · 12 comments
Closed

Typescript typeof invalid formatting #14283

dfadev opened this issue Feb 1, 2023 · 12 comments
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Comments

@dfadev
Copy link

dfadev commented Feb 1, 2023

Prettier 2.8.3
Playground link

--parser typescript
--no-semi
--single-quote
--jsx-single-quote

Input:

function a() {
  const animals = [
  	{ species: 'cat', name: 'Fluffy' },
  	{ species: 'dog', name: 'Fido' },
  	{ species: 'mouse', name: 'Trevor' }
  ] as const
  
  type Animal = typeof animals[number]['species']
  // type Animal = "cat" | "dog" | "mouse"  
}

Output:

function a() {
  const animals = [
    { species: 'cat', name: 'Fluffy' },
    { species: 'dog', name: 'Fido' },
    { species: 'mouse', name: 'Trevor' },
  ] as const

  type Animal = (typeof animals)[number]['species']
  // type Animal = "cat" | "dog" | "mouse"
}

Expected behavior:

function a() {
  const animals = [
    { species: 'cat', name: 'Fluffy' },
    { species: 'dog', name: 'Fido' },
    { species: 'mouse', name: 'Trevor' },
  ] as const

  type Animal = (typeof animals[number]['species'])
  // type Animal = "cat" | "dog" | "mouse"
}

see also https://steveholgado.com/typescript-types-from-arrays/

@fisker
Copy link
Sponsor Member

fisker commented Feb 1, 2023

This change is intended, see #13546 and #14042

@fisker fisker closed this as completed Feb 1, 2023
@EladBezalel
Copy link

@fisker it will be much appreciated if we could have a config to disable this rule,
ATM lints are failing on unchanged files since this change was introduced

@sosukesuzuki
Copy link
Member

@EladBezalel Could you show us codes that was broken by this change? This change is a just bug fix, so I'm wonder why your code is broken.

@EladBezalel
Copy link

EladBezalel commented Feb 7, 2023

The code has typeof·HANDLED_KEYS and now after upgrading deps we get these kind of errors

error  Replace `typeof·HANDLED_KEYS` with `(typeof·HANDLED_KEYS)`  prettier/prettier

on files that didn't changed

@kachkaev
Copy link
Member

kachkaev commented Feb 7, 2023

@EladBezalel this is expected after upgrading from 2.8.2 to 2.8.3. See see #13546 and #14042.

@EladBezalel
Copy link

i understand this is expected, i suggest a config to disable it

@kachkaev
Copy link
Member

kachkaev commented Feb 7, 2023

New config is not planned, according to the Prettier’s Option Philosophy. If you use Prettier via ESLint, I guess you can apply some fixes post-Prettier. However, it might be best to just stick with the new (improved) behavior and thus keep the codebase in sync with the community. It’s not so much about better or worse, it’s more about the same everywhere.

@graup
Copy link

graup commented May 10, 2023

As far as I can tell, #13546 doesn't provide any reasoning what's bad about type A = typeof foo[number], could someone help me? There's this comment but it doesn't give a reason against the no-braces version.

I'm not against this, just curious why it is needed. (Would make sense to comment on the issue, but it is sadly locked.)

@fisker
Copy link
Sponsor Member

fisker commented May 10, 2023

type  a = typeof a[a];
const a = typeof a[a];

This is confusing for ts beginner.

@graup
Copy link

graup commented May 10, 2023

I guess I can see how there is a small difference in readability.

// Before
const a = [1];
const B = typeof a[0];  // B = 'number'
type A = typeof a[number];  // type A = number

// After
const a = [1];
const B = typeof a[0];
type A = (typeof a)[number];

But was there any data suggesting people found it confusing? Seems a bit arbitrary to just change this based on a hunch.

@fisker
Copy link
Sponsor Member

fisker commented May 10, 2023

I'm the beginner, prettier/yaml-unist-parser#281 (comment)

@graup
Copy link

graup commented May 10, 2023

Thanks, appreciate the history!

const tuple = ['a', 'b'] as const;
type T = typeof tuple[number];

This is a pretty common pattern in TS, so a lot of people that installed Prettier in the last 3 years and update now will be wondering why they have to update their code :D

@github-actions github-actions bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Nov 25, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.
Projects
None yet
Development

No branches or pull requests

6 participants