From ccfd68e365391b3f117df96792355f9c3655288c Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Mon, 5 Apr 2021 00:54:19 -0400 Subject: [PATCH] fix(eslint-plugin): [restrict-plus-operands] consider template literal types as strings (#3234) --- packages/eslint-plugin/src/rules/restrict-plus-operands.ts | 5 ++++- .../tests/rules/restrict-plus-operands.test.ts | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts index 0edf8a37d20..0f3b07e9fb2 100644 --- a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts +++ b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts @@ -57,7 +57,10 @@ export default util.createRule({ if (type.isNumberLiteral()) { return 'number'; } - if (type.isStringLiteral()) { + if ( + type.isStringLiteral() || + util.isTypeFlagSet(type, ts.TypeFlags.TemplateLiteral) + ) { return 'string'; } // is BigIntLiteral diff --git a/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts b/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts index daa02fa46d9..af33e3ad3db 100644 --- a/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts +++ b/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts @@ -110,6 +110,12 @@ const x = a + b; declare const a: 'string literal' & string; declare const b: string; const x = a + b; + `, + ` +function A(s: string) { + return \`a\${s}b\` as const; +} +const b = A('') + '!'; `, { code: `