Flow: $Shape should mark fields as optional, not required

Created on 9 Jul 2019  路  4Comments  路  Source: facebook/flow

````js
// @flow
/* flowlint

type Shape = $ObjMap(param: T) => T | void | null>;
type B = { a: { b: 1 } };
declare var obj: Shape;
obj.a?.b; // Does not error, correctly.

declare var obj2: $Shape;
obj2.a?.b; // Should not error but does.
````
[try flow]

One solution might be to replace the $Shape utility with one implemented using $ObjMap instead

duplicate bug needs triage

All 4 comments

There is better one #7643

declare var obj2: $Shape<B>;
obj2.a?.b; // Should not error but does.

Yeah, $Shape doesn't really make fields optional, and it is kinda expected behaviour. It has been discussed many times. See #7730, #7566. Also #7316, #5702, #6039, #6796 (open issues).

const a: $Shape<{ a: 1, b: 2 }> = { a: 1 } // no error
a.b * 2 // no error

I guess, what $Shape really does is something like reversing direction of width subtyping (and that's unsafe). You can use $Rest<T, {}> or {...T} instead.

This can be closed as a duplicate, I think.

Good info @Bannerets. Will close as duplicate.

Duplicate of #5702

Was this page helpful?
0 / 5 - 0 ratings

Related issues

funtaps picture funtaps  路  3Comments

bennoleslie picture bennoleslie  路  3Comments

ghost picture ghost  路  3Comments

mmollaverdi picture mmollaverdi  路  3Comments

mjj2000 picture mjj2000  路  3Comments