Skip to content

feat(eslint-plugin): [restrict-plus-operands] add intersection type determination logic #2628

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

Merged
merged 6 commits into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/eslint-plugin/src/rules/restrict-plus-operands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export default util.createRule<Options, MessageIds>({
return types.every(value => value === types[0]) ? types[0] : 'invalid';
}

if (type.isIntersection()) {
const types = type.types.map(getBaseTypeOfLiteralType);
return types.some(value => value === 'string') ? 'string' : 'invalid';
}
Comment on lines +73 to +76
Copy link
Member

Choose a reason for hiding this comment

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

it took me a second to figure out why this works for valid cases.
For the weird cases we care about (like {} & string), typescript tells us the type is an intersection type.

For other cases like unknown & string, typescript tells us the type is just string!
And for bad cases like number & string, typescript again tells us the type is just never!

This is pretty awesome! 😄


const stringType = typeChecker.typeToString(type);

if (
Expand Down
118 changes: 118 additions & 0 deletions packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ function foo<T extends number>(a: T) {
function foo<T extends 1>(a: T) {
return a + 1;
}
`,
`
declare const a: {} & string;
declare const b: string;
const x = a + b;
`,
`
declare const a: unknown & string;
declare const b: string;
const x = a + b;
`,
`
declare const a: string & string;
declare const b: string;
const x = a + b;
`,
`
declare const a: 'string literal' & string;
declare const b: string;
const x = a + b;
`,
{
code: `
Expand Down Expand Up @@ -411,6 +431,104 @@ function foo<T extends 1>(a: T) {
},
],
},
{
code: `
declare const a: boolean & string;
declare const b: string;
const x = a + b;
`,
errors: [
{
messageId: 'notStrings',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: number & string;
declare const b: string;
const x = a + b;
`,
errors: [
{
messageId: 'notStrings',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: symbol & string;
declare const b: string;
const x = a + b;
`,
errors: [
{
messageId: 'notStrings',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: object & string;
declare const b: string;
const x = a + b;
`,
errors: [
{
messageId: 'notStrings',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: never & string;
declare const b: string;
const x = a + b;
`,
errors: [
{
messageId: 'notStrings',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: any & string;
declare const b: string;
const x = a + b;
`,
errors: [
{
messageId: 'notStrings',
line: 4,
column: 19,
},
],
},
{
code: `
declare const a: { a: 1 } & { b: 2 };
declare const b: string;
const x = a + b;
`,
errors: [
{
messageId: 'notStrings',
line: 4,
column: 19,
},
],
},
{
code: `
let foo: string | undefined;
Expand Down