File tree 2 files changed +89
-3
lines changed
2 files changed +89
-3
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ export default util.createRule({
9
9
docs : {
10
10
description :
11
11
'When adding two variables, operands must both be of type number or of type string.' ,
12
- tslintRuleName : 'restrict-plus-operands' ,
12
+ tslintName : 'restrict-plus-operands' ,
13
13
category : 'Best Practices' ,
14
14
recommended : false ,
15
15
} ,
@@ -32,10 +32,18 @@ export default util.createRule({
32
32
33
33
/**
34
34
* Helper function to get base type of node
35
- * @param type type to be evaluated
36
- * @returns string, number or invalid
37
35
*/
38
36
function getBaseTypeOfLiteralType ( type : ts . Type ) : BaseLiteral {
37
+ const constraint = type . getConstraint ( ) ;
38
+ if (
39
+ constraint &&
40
+ // for generic types with union constraints, it will return itself from getConstraint
41
+ // so we have to guard against infinite recursion...
42
+ constraint !== type
43
+ ) {
44
+ return getBaseTypeOfLiteralType ( constraint ) ;
45
+ }
46
+
39
47
if ( type . isNumberLiteral ( ) ) {
40
48
return 'number' ;
41
49
}
Original file line number Diff line number Diff line change @@ -60,6 +60,27 @@ var foo = ("5.5" as string) + pair.second;
60
60
`const foo = 'hello' + (someBoolean ? 'a' : 'b') + (() => someBoolean ? 'c' : 'd')() + 'e';` ,
61
61
`const balls = true;` ,
62
62
`balls === true;` ,
63
+ // https://github.com/typescript-eslint/typescript-eslint/issues/230
64
+ `
65
+ function foo<T extends string>(a: T) {
66
+ return a + "";
67
+ }
68
+ ` ,
69
+ `
70
+ function foo<T extends "a" | "b">(a: T) {
71
+ return a + "";
72
+ }
73
+ ` ,
74
+ `
75
+ function foo<T extends number>(a: T) {
76
+ return a + 1;
77
+ }
78
+ ` ,
79
+ `
80
+ function foo<T extends 1>(a: T) {
81
+ return a + 1;
82
+ }
83
+ ` ,
63
84
] ,
64
85
invalid : [
65
86
{
@@ -305,5 +326,62 @@ var foo = pair + pair;
305
326
} ,
306
327
] ,
307
328
} ,
329
+ // https://github.com/typescript-eslint/typescript-eslint/issues/230
330
+ {
331
+ code : `
332
+ function foo<T extends string>(a: T) {
333
+ return a + 1;
334
+ }
335
+ ` ,
336
+ errors : [
337
+ {
338
+ messageId : 'notStrings' ,
339
+ line : 3 ,
340
+ column : 12 ,
341
+ } ,
342
+ ] ,
343
+ } ,
344
+ {
345
+ code : `
346
+ function foo<T extends "a" | "b">(a: T) {
347
+ return a + 1;
348
+ }
349
+ ` ,
350
+ errors : [
351
+ {
352
+ messageId : 'notStrings' ,
353
+ line : 3 ,
354
+ column : 12 ,
355
+ } ,
356
+ ] ,
357
+ } ,
358
+ {
359
+ code : `
360
+ function foo<T extends number>(a: T) {
361
+ return a + "";
362
+ }
363
+ ` ,
364
+ errors : [
365
+ {
366
+ messageId : 'notStrings' ,
367
+ line : 3 ,
368
+ column : 12 ,
369
+ } ,
370
+ ] ,
371
+ } ,
372
+ {
373
+ code : `
374
+ function foo<T extends 1>(a: T) {
375
+ return a + "";
376
+ }
377
+ ` ,
378
+ errors : [
379
+ {
380
+ messageId : 'notStrings' ,
381
+ line : 3 ,
382
+ column : 12 ,
383
+ } ,
384
+ ] ,
385
+ } ,
308
386
] ,
309
387
} ) ;
You can’t perform that action at this time.
0 commit comments