8000 fix(eslint-plugin): [no-base-to-string] support boolean in unions (#1… · dandv/typescript-eslint@6987ecc · GitHub
Skip to content

Commit 6987ecc

Browse files
authored
fix(eslint-plugin): [no-base-to-string] support boolean in unions (typescript-eslint#1979)
1 parent 56d9870 commit 6987ecc

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

packages/eslint-plugin/src/rules/no-base-to-string.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,27 @@ export default util.createRule<Options, MessageIds>({
123123
return Usefulness.Never;
124124
}
125125

126+
let allSubtypesUseful = true;
127+
let someSubtypeUseful = false;
128+
126129
for (const subType of type.types) {
127-
if (collectToStringCertainty(subType) !== Usefulness.Never) {
128-
return Usefulness.Sometimes;
130+
const subtypeUsefulness = collectToStringCertainty(subType);
131+
132+
if (subtypeUsefulness !== Usefulness.Always && allSubtypesUseful) {
133+
allSubtypesUseful = false;
129134
}
135+
136+
if (subtypeUsefulness !== Usefulness.Never && !someSubtypeUseful) {
137+
someSubtypeUseful = true;
138+
}
139+
}
140+
141+
if (allSubtypesUseful && someSubtypeUseful) {
142+
return Usefulness.Always;
143+
}
144+
145+
if (someSubtypeUseful) {
146+
return Usefulness.Sometimes;
130147
}
131148

132149
return Usefulness.Never;

packages/eslint-plugin/tests/rules/no-base-to-string.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ const literalWithToString = {
7878
toString: () => 'Hello, world!',
7979
};
8080
'' + literalToString;
81+
`,
82+
`
83+
const printer = (inVar: string | number | boolean) => {
84+
inVar.toString();
85+
};
86+
printer('');
87+
printer(1);
88+
printer(true);
8189
`,
8290
'let _ = {} * {};',
8391
'let _ = {} / {};',

0 commit comments

Comments
 (0)
0