File tree 1 file changed +38
-0
lines changed
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -103,3 +103,41 @@ function example(x: Union) {
103
103
if ( is ( x ) ) { }
104
104
x ; // Union
105
105
}
106
+
107
+ function checksArrayOrObject1 ( obj : Record < string , any > | Record < string , any > [ ] ) {
108
+ // "accidentally" guards the first branch on the length
109
+ if ( Array . isArray ( obj ) && obj . length ) {
110
+ for ( let key in obj ) {
111
+ if ( obj [ key ] !== undefined ) {
112
+ console . log ( obj [ key ] )
113
+ }
114
+ }
115
+ }
116
+ else {
117
+ // 'obj' should probably not include an array type here.
118
+ for ( let key in obj ) {
119
+ if ( obj [ key ] !== undefined ) {
120
+ console . log ( obj [ key ] )
121
+ }
122
+ }
123
+ }
124
+ }
125
+
126
+ function checksArrayOrObject2 ( obj : Record < string , any > | Record < string , any > [ ] ) {
127
+ if ( Array . isArray ( obj ) ) {
128
+ // obj should only be an array type here
129
+ for ( let key in obj ) {
130
+ if ( obj [ key ] !== undefined ) {
131
+ console . log ( obj [ key ] )
132
+ }
133
+ }
134
+ }
135
+ else {
136
+ // 'obj' should probably not include an array type here.
137
+ for ( let key in obj ) {
138
+ if ( obj [ key ] !== undefined ) {
139
+ console . log ( obj [ key ] )
140
+ }
141
+ }
142
+ }
143
+ }
You can’t perform that action at this time.
0 commit comments