Flow: Indexer Property on interface doesn't work

Created on 17 May 2018  Â·  1Comment  Â·  Source: facebook/flow

https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVBLAdgFwKYBOUAhgMZ5gDyADjhnFgM5gDeqYYAbsQRsQEYw8jAPwAuVmADaAazwBPCYxy8sAcwC6E4lnlgAvgG52YODgAWhcWCwBXALb9Cx-SdTBgVGalINlp2nomAEYJGjo-MABeSTNLAglgg3QPMABRAgI4Ah8-HACIpgAmMMDImLYObl4BIUYJSo4wS0UwAHJzDDaTV1cgA

/* @flow */

interface Options {
  variables?: { [key: string]: any };
  other?: number;
}

// Ok
const options1: Options = { other: 1 }

// Error
const options2: Options = {
  variables: {
    hey: 'hi'
  }
}
13:   variables: {                 ^ Cannot assign object literal to `options2` because an indexer property is missing in object literal [1] but exists in object type [2] in property `variables`.
References:
13:   variables: {                 ^ [1]
4:   variables?: { [key: string]: any };
                 ^ [2]

This will work okay by simply changing interface Options { to type Options = {

interfaces bug

Most helpful comment

Options should not be declared as interfaces IMHO.
Also – as of today – I think the correct way to extend an option with another one is using spreads.

  declare type ModifiableWatchQueryOptions = {
    variables?: { [key: string]: any };
    pollInterval?: number;
    fetchPolicy?: FetchPolicy;
    errorPolicy?: ErrorPolicy;
    fetchResults?: boolean;
    notifyOnNetworkStatusChange?: boolean;
  }

  declare type WatchQueryOptions = {
    ...ModifiableWatchQueryOptions,
    query: DocumentNode;
    metadata?: any;
    context?: any;
  }

>All comments

Options should not be declared as interfaces IMHO.
Also – as of today – I think the correct way to extend an option with another one is using spreads.

  declare type ModifiableWatchQueryOptions = {
    variables?: { [key: string]: any };
    pollInterval?: number;
    fetchPolicy?: FetchPolicy;
    errorPolicy?: ErrorPolicy;
    fetchResults?: boolean;
    notifyOnNetworkStatusChange?: boolean;
  }

  declare type WatchQueryOptions = {
    ...ModifiableWatchQueryOptions,
    query: DocumentNode;
    metadata?: any;
    context?: any;
  }
Was this page helpful?
0 / 5 - 0 ratings