@@ -301,3 +301,104 @@ type Foo = { [key: string]: unknown };
301
301
type NullableFoo = Foo | undefined ;
302
302
303
303
type Bar < T extends NullableFoo > = NonNullable < T > [ string ] ;
304
+
305
+ // Generics and intersections with {}
306
+
307
+ function fx0 < T > ( value : T & ( { } | null ) ) {
308
+ if ( value === 42 ) {
309
+ value ; // T & {}
310
+ }
311
+ else {
312
+ value ; // T & ({} | null)
313
+ }
314
+ }
315
+
316
+ function fx1 < T extends unknown > ( value : T & ( { } | null ) ) {
317
+ if ( value === 42 ) {
318
+ value ; // T & {}
319
+ }
320
+ else {
321
+ value ; // T & ({} | null)
322
+ }
323
+ }
324
+
325
+ function fx2 < T extends { } > ( value : T & ( { } | null ) ) {
326
+ if ( value === 42 ) {
327
+ value ; // T & {}
328
+ }
329
+ else {
330
+ value ; // T & ({} | null)
331
+ }
332
+ }
333
+
334
+ function fx3 < T extends { } | undefined > ( value : T & ( { } | null ) ) {
335
+ if ( value === 42 ) {
336
+ value ; // T & {}
337
+ }
338
+ else {
339
+ value ; // T & ({} | null)
340
+ }
341
+ }
342
+
343
+ function fx4 < T extends { } | null > ( value : T & ( { } | null ) ) {
344
+ if ( value === 42 ) {
345
+ value ; // T & {}
346
+ }
347
+ else {
348
+ value ; // T & ({} | null)
349
+ }
350
+ }
351
+
352
+ function fx5 < T extends { } | null | undefined > ( value : T & ( { } | null ) ) {
353
+ if ( value === 42 ) {
354
+ value ; // T & {}
355
+ }
356
+ else {
357
+ value ; // T & ({} | null)
358
+ }
359
+ }
360
+
361
+ // Double-equals narrowing
362
+
363
+ function fx10 ( x : string | number , y : number ) {
364
+ if ( x == y ) {
365
+ x ; // string | number
366
+ }
367
+ else {
368
+ x ; // string | number
369
+ }
370
+ if ( x != y ) {
371
+ x ; // string | number
372
+ }
373
+ else {
374
+ x ; // string | number
375
+ }
376
+ }
377
+
378
+ // Repros from #50706
379
+
380
+ function SendBlob ( encoding : unknown ) {
381
+ if ( encoding !== undefined && encoding !== 'utf8' ) {
382
+ throw new Error ( 'encoding' ) ;
383
+ }
384
+ encoding ;
385
+ } ;
386
+
387
+ function doSomething1 < T extends unknown > ( value : T ) : T {
388
+ if ( value === undefined ) {
389
+ return value ;
390
+ }
391
+ if ( value === 42 ) {
392
+ throw Error ( 'Meaning of life value' ) ;
393
+ }
394
+ return value ;
395
+ }
396
+
397
+ function doSomething2 ( value : unknown ) : void {
398
+ if ( value === undefined ) {
399
+ return ;
400
+ }
401
+ if ( value === 42 ) {
402
+ value ;
403
+ }
404
+ }
0 commit comments