Flow: Remove property from object type

Created on 17 Nov 2016  路  4Comments  路  Source: facebook/flow

Is there a way to remove a property from an existing type without creating a new type without that property?

Contrived example:

type ArgType = {
    name: string,
    id: string,
    count: number,
};
const RemoveName = ({name, ...rest}: ArgType) => rest;

Can I create a result type here from ArgType?

Most helpful comment

$Diff doesn't behave properly in this use case, see https://github.com/facebook/flow/issues/2781

type Foo = {
  foo: string,
  bar: string
};

const bar: $Diff<Foo, { foo: any }> = { bar: undefined }; // no errors

All 4 comments

No, you can't do that at the moment

Actually, scratch that, you can do that:

type Foo = {
  foo: string,
  bar: string
}

const bar: $Diff<Foo, { foo: any }> = { bar: '123' };

$Diff doesn't behave properly in this use case, see https://github.com/facebook/flow/issues/2781

type Foo = {
  foo: string,
  bar: string
};

const bar: $Diff<Foo, { foo: any }> = { bar: undefined }; // no errors

Does it not work for polymorphic types?

````js
/* @flow */

function foo>(t: T, u: U, v: V): void {}

foo(
{ name: '', age: 1 },
{ age: 1 },
{} // Should Error: 'name' prop is ommited
)

function bar

bar(
{}, // Correctly Errors
)
````

https://flowtype.org/try/#0PQKgBAAgZgNg9gdzCYAoVUCuA7AxgFwEs5swo44AeAFQBowBVegNQC4wASAEUKihvoMAfEIAU+dnTCZ2TMADd2zAJTt5cQgBMwAbwC+qMEeMnTRjBVGHdYbAEMAtgFN2Aclf07AcxdgAjGB6tNY6YN6+AUEhemDAwGAAygAWcJgw2gCiAE5ZcFlu9s6uYAAOuSVghADOYHAODoT4TpqoyuhYeEQkYABGdlmUbJw8fJShhb5V+FmE2F6ePuzYmA49TlmB9KHhSytrG3oioopgKmoa2vrWZjfmfVlWRvr0cWAAwnlZTgQwAJ5g2VyWSqrSAA

Was this page helpful?
0 / 5 - 0 ratings