Search code examples
flow-js

How to combine flowjs type definitions


How can I combine the following type definitions, so I dont have to repeat myself?

type Lama = {|
name: string,
|};

type LamaWithHat = {|
name: string,
hat: string,
|};

Tried using Interseptions but appearently the syntax might be wrong:

type LamaWithHat = {|
hat: string,
|} & Lama;

Solution

  • How about if we do this /* @flow */

    type Lama = {|
    name: string,
    |};
    
    type LamaWithHat = {|
    ...Lama,
    hat: string,
    |};
    
    const a: Lama = {
        name: 'rahul'
    }
    
    const b: LamaWithHat = {
        name: 'rahul',
        hat: 'circluar'
    }
    

    test this code here https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgAyAhgLbFgC8YA3gD6oB2ZeAXGAM4YBOAlowHMANKjoBfANzps+ImWIB1XhgAWACWIYqtBgDp9JciJWb2XPoJHipqAMZxGXMMXaGK1GqjDewzUmzAAcm5iFQBXGEDUMXR7Ry0AI1d5JVUNLQ8vHz8A4NCIwJEfMBMMdkDbXm5bGDDibiixIA