Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [no-unnecessary-condition] check type predicates based on assignability#11799
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
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
0ae067d5cf27d8fa3a3b2dc700c8c17df5cFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import * as tsutils from 'ts-api-utils'; | ||
| import * as ts from 'typescript'; | ||
| /** | ||
| * Returns whether the type is a fresh object literal, which will be subject to | ||
| * excess property checking. | ||
| * | ||
| * For example, in the following snippet, the first function call has an object | ||
| * literal type, and therefore is disallowed by excess property checking, | ||
| * whereas the second function call is allowed, because the argument is not an | ||
| * object literal type: | ||
| * | ||
| * ```ts | ||
| * declare function f(x: { a: string }); | ||
| * | ||
| * f({ a: 'foo', excess: 'property' }); // TS error | ||
| * | ||
| * const allowed = { a: 'foo', excess: 'property' }; | ||
| * | ||
| * f(allowed); // allowed | ||
| * ``` | ||
| */ | ||
| export function isObjectLiteralType(type: ts.Type): boolean { | ||
| return ( | ||
| tsutils.isObjectType(type) && | ||
| tsutils.isObjectFlagSet(type, ts.ObjectFlags.ObjectLiteral) | ||
| ); | ||
| } | ||
| /** | ||
| * Maps object literal types into ordinary types, in order to be able to avoid | ||
| * spurious results from `checker.isTypeAssignableTo()` due to excess property | ||
| * checking. | ||
| */ | ||
| export function toWidenedType(checker: ts.TypeChecker, type: ts.Type): ts.Type { | ||
| return isObjectLiteralType(type) ? checker.getWidenedType(type) : type; | ||
| } |
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.