Skip to content

Commit 1f868ce

Browse files
authored
fix(eslint-plugin): use bracket for infer type in array-type rule (#173)
1 parent f0dddbc commit 1f868ce

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

packages/eslint-plugin/lib/rules/array-type.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function typeNeedsParentheses(node) {
6767
case 'TSFunctionType':
6868
case 'TSIntersectionType':
6969
case 'TSTypeOperator':
70+
case 'TSInferType':
7071
return true;
7172
default:
7273
return false;

packages/eslint-plugin/tests/lib/rules/array-type.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ ruleTester.run('array-type', rule, {
138138
}`,
139139
options: ['array']
140140
},
141+
{
142+
// https://github.com/typescript-eslint/typescript-eslint/issues/172
143+
code: 'type Unwrap<T> = T extends (infer E)[] ? E : T',
144+
options: ['array']
145+
},
141146
{
142147
code: `let z: Array = [3, "4"];`,
143148
options: ['generic']
@@ -173,6 +178,11 @@ ruleTester.run('array-type', rule, {
173178
{
174179
code: `type fooIntersection = Array<string & number>;`,
175180
options: ['generic']
181+
},
182+
{
183+
// https://github.com/typescript-eslint/typescript-eslint/issues/172
184+
code: 'type Unwrap<T> = T extends Array<infer E> ? E : T',
185+
options: ['generic']
176186
}
177187
],
178188
invalid: [
@@ -774,6 +784,34 @@ let yyyy: Arr<Array<Array<Arr<string>>>> = [[[["2"]]]];`,
774784
column: 19
775785
}
776786
]
787+
},
788+
{
789+
// https://github.com/typescript-eslint/typescript-eslint/issues/172
790+
code: 'type Unwrap<T> = T extends Array<infer E> ? E : T',
791+
output: 'type Unwrap<T> = T extends (infer E)[] ? E : T',
792+
options: ['array'],
793+
errors: [
794+
{
795+
messageId: 'errorStringArray',
796+
data: { type: 'T' },
797+
line: 1,
798+
column: 28
799+
}
800+
]
801+
},
802+
{
803+
// https://github.com/typescript-eslint/typescript-eslint/issues/172
804+
code: 'type Unwrap<T> = T extends (infer E)[] ? E : T',
805+
output: 'type Unwrap<T> = T extends Array<infer E> ? E : T',
806+
options: ['generic'],
807+
errors: [
808+
{
809+
messageId: 'errorStringGeneric',
810+
data: { type: 'T' },
811+
line: 1,
812+
column: 28
813+
}
814+
]
777815
}
778816
]
779817
});

0 commit comments

Comments
 (0)