Skip to content

Support intersection of array type and object typeΒ #1330

@samwgoldman

Description

@samwgoldman
/* @flow */                                                                                                                           

type T = { foo: string } & number[];                                                                                                  

function foo(): T {                                                                                                                   
  let x = [];                                                                                                                         
  x.foo = "";                                                                                                                         
  return x; // Can't create an inhabitant                                                                                             
}                                                                                                                                     

function foo(x: T): void {                                                                                                            
  // $ExpectError x.foo is a string, not a number                                                                                     
  (x.foo : number);                                                                                                                   

  // $ExpectError x.length is a number (from Array.prototype.length), not a string                                                    
  (x.length : string);                                                                                                                

  // $ExpectError x[0] is a number, not a string                                                                                      
  (x[0] : string);                                                                                                                    
}                                                                                                                                     
test.js:7
  7:   x.foo = "";
         ^^^ property `foo`. Property not found in
  7:   x.foo = "";
       ^^^^^ Array

test.js:8
  8:   return x; // Can't create an inhabitant
              ^ empty array literal. This type is incompatible with
  3: type T = { foo: string } & number[];
              ^^^^^^^^^^^^^^^ object type

test.js:18
 18:   // $ExpectError x[0] is a number, not a string
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Error suppressing comment. Unused suppression

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions