Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(eslint-plugin): [consistent-indexed-object-style] report mapped types#10160

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

kirkwaiblinger
Copy link
Member

@kirkwaiblingerkirkwaiblinger commentedOct 16, 2024
edited
Loading

PR Checklist

Overview

This reports on mapped types that can be converted toRecords.However, note that not all mapped types can be converted toRecords, so the rule only reports when they can be converted.

// can't be converted to record, because `K` is used in the computed type. Not reported.typeFoo1={[KinT]:K};// Can be converted to record. Reported.typeFoo2={[KinT]:T}// fixes totypeFoo2=Record<T,T>// Can be converted to record. Reported.typeWithModifiers={+readonly[keyinT]+?:T}// fixes totypeWithModifiers=Readonly<Partial<Record<T,T>>>// WEIRD CASE: Cannot be converted to Record due to keyof (mapped type syntax preserves readonly modifiers in the keys of T)typeWithKeyof={[KinkeyofT]:T}// WEIRD CASE: Can be converted to Record due to keyof being parenthesizedtypeWithKeyof={[Kin(keyofT)]:T}

For more detail on the "weird case", study the following example. (Thanks to@Retsam for the helpful discord conversation inhttps://discord.com/channels/508357248330760243/508357638677856287/1296364542498177046!)

typeMyObject={required:string,optional?:number,}typeBooleanValuesNoParens={[keyinkeyofMyObject]:boolean}// validconstbv:BooleanValuesNoParens={required:true,}typeBooleanValuesWithParens={[keyin(keyofMyObject)]:boolean}// TS ERROR: Property 'optional' is missingconstbv:BooleanValuesWithParens={required:true,}

This actually has practical application inside our repo!

):{
// intentionally not using a Record to preserve optionals
[kinkeyofParseResult]:unknown;
};


There isone open question: should we report on cases where the-readonly modifier is used? I've gone with the approach for now that we should report it even though we can't provide a fix. Open to differing opinions on this, though.

typePlusReadonly={+readonly[KinT]:T}// equivalent totypePlusReadonly=Readonly<Record<T,T>>typePlusOptional={[KinT]+?:T}// equivalent totypePlusOptional=Partial<Record<T,T>>typeMinusOptional={[KinT]-?:T}// equivalent totypeMinusOptional=Required<Record<T,T>>typeMinusReadonly={-readonly[KinT]:T}// no built-in equivalent :( You can write your own easily though.typeMutable<T>={-readonly[KinkeyofT]:T[K]}// then,typeMinusReadonly=Mutable<Record<T,T>>

@typescript-eslint
Copy link
Contributor

Thanks for the PR,@kirkwaiblinger!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently onhttps://opencollective.com/typescript-eslint.

@netlifyNetlify
Copy link

netlifybot commentedOct 16, 2024
edited
Loading

Deploy Preview fortypescript-eslint ready!

NameLink
🔨 Latest commit90b9c25
🔍 Latest deploy loghttps://app.netlify.com/sites/typescript-eslint/deploys/67116ce26227610007f7daf8
😎 Deploy Previewhttps://deploy-preview-10160--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 98 (🔴 down 1 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 90 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to yourNetlify site configuration.

@nx-cloudNx Cloud
Copy link

nx-cloudbot commentedOct 16, 2024
edited
Loading

☁️ Nx Cloud Report

CI is running/has finished running commands for commit90b9c25. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 fromNxCloud.

@kirkwaiblinger

This comment was marked as outdated.

@codecovCodecov
Copy link

codecovbot commentedOct 17, 2024
edited
Loading

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.17%. Comparing base(7effdea) to head(90b9c25).
Report is 17 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@##             main   #10160      +/-   ##==========================================+ Coverage   86.15%   86.17%   +0.02%==========================================  Files         429      429                Lines       15005    15029      +24       Branches     4353     4360       +7     ==========================================+ Hits        12927    12951      +24  Misses       1729     1729                Partials      349      349
FlagCoverage Δ
unittest86.17% <100.00%> (+0.02%)⬆️

Flags with carried forward coverage won't be shown.Click here to find out more.

Files with missing linesCoverage Δ
...lugin/src/rules/consistent-indexed-object-style.ts97.59% <100.00%> (+0.98%)⬆️
...-plugin/src/rules/restrict-template-expressions.ts100.00% <ø> (ø)
packages/eslint-plugin/src/rules/typedef.ts98.61% <ø> (ø)
...ckages/scope-manager/src/referencer/VisitorBase.ts88.23% <ø> (ø)

@kirkwaiblingerkirkwaiblinger marked this pull request as ready for reviewOctober 17, 2024 19:55
Copy link
Member

@JoshuaKGoldbergJoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Looks great to me, thanks! 🚀

Excited to have this extra bit of consistency.

TypeScript supports defining arbitrary object keys using an index signature. TypeScript also has a builtin type named `Record` to create an empty object defining only an index signature. For example, the following types are equal:
TypeScript supports defining arbitrary object keys using an index signature or mapped type.
TypeScript also has a builtin type named `Record` to create an empty object defining only an index signature.
For example, the following types are equal:

Choose a reason for hiding this comment

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

[Praise] 😄 I like the.\n splitting

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Did it just for you 😁

JoshuaKGoldberg reacted with laugh emojiJoshuaKGoldberg reacted with heart emoji
@JoshuaKGoldbergJoshuaKGoldberg added the 1 approval>=1 team member has approved this PR; we're now leaving it open for more reviews before we merge labelOct 21, 2024
@JoshuaKGoldbergJoshuaKGoldberg merged commit9c956ee intotypescript-eslint:mainOct 23, 2024
64 checks passed
@kirkwaiblingerkirkwaiblinger deleted the prefer-record-over-in branchOctober 23, 2024 22:48
renovatebot added a commit to mmkal/eslint-plugin-mmkal that referenced this pull requestOct 28, 2024
##### [v8.12.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8120-2024-10-28)##### 🚀 Features-   **eslint-plugin:** \[no-base-to-string] handle String() ([#10005](typescript-eslint/typescript-eslint#10005))-   **eslint-plugin:** \[switch-exhaustiveness-check] add allowDefaultCaseMatchUnionMember option ([#9954](typescript-eslint/typescript-eslint#9954))-   **eslint-plugin:** \[consistent-indexed-object-style] report mapped types ([#10160](typescript-eslint/typescript-eslint#10160))-   **eslint-plugin:** \[prefer-nullish-coalescing] add support for assignment expressions ([#10152](typescript-eslint/typescript-eslint#10152))##### ❤️  Thank You-   Abraham Guo-   Kim Sang Du [@developer-bandi](https://github.com/developer-bandi)-   Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger)-   YeonJuan [@yeonjuan](https://github.com/yeonjuan)You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
renovatebot added a commit to andrei-picus-tink/auto-renovate that referenced this pull requestOct 30, 2024
| datasource | package                          | from   | to     || ---------- | -------------------------------- | ------ | ------ || npm        | @typescript-eslint/eslint-plugin | 8.11.0 | 8.12.2 || npm        | @typescript-eslint/parser        | 8.11.0 | 8.12.2 |## [v8.12.2](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8122-2024-10-29)##### 🩹 Fixes-   **eslint-plugin:** \[switch-exhaustiveness-check] invert `considerDefaultExhaustiveForUnions` ([#10223](typescript-eslint/typescript-eslint#10223))##### ❤️  Thank You-   Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger)You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.## [v8.12.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8121-2024-10-28)This was a version bump only for eslint-plugin to align it with other projects, there were no code changes.You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.## [v8.12.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8120-2024-10-28)##### 🚀 Features-   **eslint-plugin:** \[no-base-to-string] handle String() ([#10005](typescript-eslint/typescript-eslint#10005))-   **eslint-plugin:** \[switch-exhaustiveness-check] add allowDefaultCaseMatchUnionMember option ([#9954](typescript-eslint/typescript-eslint#9954))-   **eslint-plugin:** \[consistent-indexed-object-style] report mapped types ([#10160](typescript-eslint/typescript-eslint#10160))-   **eslint-plugin:** \[prefer-nullish-coalescing] add support for assignment expressions ([#10152](typescript-eslint/typescript-eslint#10152))##### ❤️  Thank You-   Abraham Guo-   Kim Sang Du [@developer-bandi](https://github.com/developer-bandi)-   Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger)-   YeonJuan [@yeonjuan](https://github.com/yeonjuan)You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
renovatebot added a commit to andrei-picus-tink/auto-renovate that referenced this pull requestOct 30, 2024
| datasource | package                          | from   | to     || ---------- | -------------------------------- | ------ | ------ || npm        | @typescript-eslint/eslint-plugin | 8.11.0 | 8.12.2 || npm        | @typescript-eslint/parser        | 8.11.0 | 8.12.2 |## [v8.12.2](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8122-2024-10-29)##### 🩹 Fixes-   **eslint-plugin:** \[switch-exhaustiveness-check] invert `considerDefaultExhaustiveForUnions` ([#10223](typescript-eslint/typescript-eslint#10223))##### ❤️  Thank You-   Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger)You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.## [v8.12.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8121-2024-10-28)This was a version bump only for eslint-plugin to align it with other projects, there were no code changes.You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.## [v8.12.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8120-2024-10-28)##### 🚀 Features-   **eslint-plugin:** \[no-base-to-string] handle String() ([#10005](typescript-eslint/typescript-eslint#10005))-   **eslint-plugin:** \[switch-exhaustiveness-check] add allowDefaultCaseMatchUnionMember option ([#9954](typescript-eslint/typescript-eslint#9954))-   **eslint-plugin:** \[consistent-indexed-object-style] report mapped types ([#10160](typescript-eslint/typescript-eslint#10160))-   **eslint-plugin:** \[prefer-nullish-coalescing] add support for assignment expressions ([#10152](typescript-eslint/typescript-eslint#10152))##### ❤️  Thank You-   Abraham Guo-   Kim Sang Du [@developer-bandi](https://github.com/developer-bandi)-   Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger)-   YeonJuan [@yeonjuan](https://github.com/yeonjuan)You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
renovatebot added a commit to andrei-picus-tink/auto-renovate that referenced this pull requestOct 31, 2024
| datasource | package                          | from   | to     || ---------- | -------------------------------- | ------ | ------ || npm        | @typescript-eslint/eslint-plugin | 8.11.0 | 8.12.2 || npm        | @typescript-eslint/parser        | 8.11.0 | 8.12.2 |## [v8.12.2](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8122-2024-10-29)##### 🩹 Fixes-   **eslint-plugin:** \[switch-exhaustiveness-check] invert `considerDefaultExhaustiveForUnions` ([#10223](typescript-eslint/typescript-eslint#10223))##### ❤️  Thank You-   Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger)You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.## [v8.12.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8121-2024-10-28)This was a version bump only for eslint-plugin to align it with other projects, there were no code changes.You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.## [v8.12.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8120-2024-10-28)##### 🚀 Features-   **eslint-plugin:** \[no-base-to-string] handle String() ([#10005](typescript-eslint/typescript-eslint#10005))-   **eslint-plugin:** \[switch-exhaustiveness-check] add allowDefaultCaseMatchUnionMember option ([#9954](typescript-eslint/typescript-eslint#9954))-   **eslint-plugin:** \[consistent-indexed-object-style] report mapped types ([#10160](typescript-eslint/typescript-eslint#10160))-   **eslint-plugin:** \[prefer-nullish-coalescing] add support for assignment expressions ([#10152](typescript-eslint/typescript-eslint#10152))##### ❤️  Thank You-   Abraham Guo-   Kim Sang Du [@developer-bandi](https://github.com/developer-bandi)-   Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger)-   YeonJuan [@yeonjuan](https://github.com/yeonjuan)You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
@github-actionsgithub-actionsbot locked asresolvedand limited conversation to collaboratorsNov 1, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@JoshuaKGoldbergJoshuaKGoldbergJoshuaKGoldberg approved these changes

@Thompson1985Thompson1985Thompson1985 approved these changes

Assignees
No one assigned
Labels
1 approval>=1 team member has approved this PR; we're now leaving it open for more reviews before we merge
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Enhancement: [consistent-indexed-object-style] forbidden from using the "in" keyword in index signature ifRecord is preferred
3 participants
@kirkwaiblinger@JoshuaKGoldberg@Thompson1985

[8]ページ先頭

©2009-2025 Movatter.jp