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): add option to no-object-literal-type-assertion rule#87

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

Merged
JamesHenry merged 8 commits intotypescript-eslint:masterfromarmano2:no-object-literal-type-assertion
Jan 22, 2019
Merged

feat(eslint-plugin): add option to no-object-literal-type-assertion rule#87

JamesHenry merged 8 commits intotypescript-eslint:masterfromarmano2:no-object-literal-type-assertion
Jan 22, 2019

Conversation

armano2
Copy link
Collaborator

This PR adds new option tono-object-literal-type-assertion to disable it when used in call or new expression.

@IlyaSemenov
fixes:#66

@codecov
Copy link

codecovbot commentedJan 20, 2019
edited
Loading

Codecov Report

Merging#87 intomaster willincrease coverage by<.01%.
The diff coverage is100%.

@@            Coverage Diff             @@##           master      #87      +/-   ##==========================================+ Coverage   95.08%   95.09%   +<.01%==========================================  Files          49       49                Lines        2483     2487       +4       Branches      371      371              ==========================================+ Hits         2361     2365       +4  Misses         73       73                Partials       49       49
Impacted FilesCoverage Δ
...ugin/lib/rules/no-object-literal-type-assertion.js100% <100%> (ø)⬆️

@JamesHenry
Copy link
Member

@armano2 Conflicts now just FYI

@armano2
Copy link
CollaboratorAuthor

i don't like name of option here:allowInCallExpression is not fully ok, this will allow to use type assertion in new and call expression....

@j-f1
Copy link
Contributor

allowAsParameter?

armano2 reacted with thumbs up emoji

@scottohara
Copy link
Contributor

There may be another use case here that needs consideration: return values.

Consider the following code:

// Returns either Thing or Partial<Thing>functiongetNewOrExistingThing(id?:number):Thing|Partial<Thing>{// if an id was not passed, return a Partial<Thing> (new)if(isNaN(id))return{ ...}asPartial<Thing>;// otherwise, return a Thing (existing)return{ ...}asThing;}

Unlike theconst x = { ... } as T ->const x: T = { ... } case, there is no equivalent workaround for return values.

Would the proposedallowAsParameter option also allow for the return value scenario above, or should there be aallowAsReturn option as well?

@j-f1
Copy link
Contributor

Since TypeScript uses a structural type system, the returned object will pass the type check as long as it has all the required properties ofThing orPartial<Thing>.

@JamesHenry
Copy link
Member

JamesHenry commentedJan 21, 2019
edited
Loading

@scottohara Following on from@j-f1 comment, I am not sure about your example.

Let's take the example where there is no extraPartial involved for simplicity:

https://typescript-play.js.org/#code/PQ18ZXTMAQDkDyAVOBxJSAiAoXAlgHYAuApgE4BmAhgMZlwoAWxA5nAN65y9xUB7AQC44AIyEAbMjSIBuHnzE0KoiQOmyFAX3xUArkTokCAonDZkSCMgHckFAKIAPAgGcTRNi3YAKAJSiPl5cihRW+hTm3Hx8giJwJBT6ZIracDRuTKxeOvh0Zh6JORwAvBZWNvZOrh7swWwBCriwrW3tHXAAQo4oKI4ASvjE5NT0jA2hsfFqUjLyirzKquJzWri6uAZGJmYV1nYOLu6e3iUATAFBJVNwBURFJNfscOUxsfxCokkpaQq84RIkXMJDyuHujwur32VSOtVODSaQA

I would say that the second form is what you would do - allowing the type assertion is really not desirable in most cases, as it opens you up to potential future issues after refactorings, and so having a linting option to allow it doesn't seem to make sense to me. Please let me know if I am missing something.

In the rare case where you do need the type assertion behaviour as a return value, using aneslint-disable comment is probably a good thing - it shows to other readers of the code (including yourself in the future) that you acknowledge you are overriding linter feedback and helps flag it up as an unusual case.

Let me know what you think

j-f1, scottohara, adidahiya, and Lonli-Lokli reacted with thumbs up emoji

Copy link
Member

@bradzacherbradzacher left a comment
edited
Loading

Choose a reason for hiding this comment

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

Code LGTM.

We can always add more cases later

armano2 reacted with thumbs up emoji
@scottohara
Copy link
Contributor

@JamesHenry - thank you for your playground example, and as usual you are 100% spot on.

The second form does indeed (at the cost of one extraconst declaration) both avoid the need for a type assertion on the return value,and conveys the developer's intent far better.

JamesHenry reacted with heart emoji

@JamesHenry
Copy link
Member

I'll cut a full release after this goes in, we've merged in quite a few fixes since v1 now

@JamesHenryJamesHenry merged commit9f501a1 intotypescript-eslint:masterJan 22, 2019
@armano2armano2 deleted the no-object-literal-type-assertion branchJanuary 22, 2019 23:50
@armano2
Copy link
CollaboratorAuthor

@JamesHenry its way easier now to test and fix issues (when we have control over all packages)

@JamesHenry
Copy link
Member

Awesome, that was the hope :) I am very happy with how our monorepo is shaping up!

I've releasedhttps://github.com/typescript-eslint/typescript-eslint/releases/tag/v1.1.0

bradzacher, platinumazure, IlyaSemenov, and j-f1 reacted with hooray emoji

armanio123 pushed a commit to armanio123/typescript-eslint that referenced this pull requestJan 24, 2019
@github-actionsgithub-actionsbot locked asresolvedand limited conversation to collaboratorsApr 21, 2020
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@bradzacherbradzacherbradzacher approved these changes

Assignees
No one assigned
Labels
enhancement: plugin rule optionNew rule option for an existing eslint-plugin rule
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

[no-object-literal-type-assertion] False positive when asserting function argument
5 participants
@armano2@JamesHenry@j-f1@scottohara@bradzacher

[8]ページ先頭

©2009-2025 Movatter.jp