Do not allow the use of non-existent keys for this type of stream

set this simple object:, { id: 'x', value: 1 }in TypeScript, if you try to do:

type foo = {
    id: string,
    v: number,
};

const bar: foo = { id: 'something', v: 1111 };

// refrencing non existent key
if (bar.xyz) {
    console.log('xyz'); 
}

You will get an error message xyz does not exist on foo. How to get the same result in Flowjs ?

I tried the following, but flowjs does not throw any errors:

type foo = {|
    id: string,
    v: number,
|};

const bar: foo = { id: 'something', v: 1111 };


if (bar.xyz) { // no errors
    console.log('xyz');
}
+4
source share
2 answers

A thread always allows checking properties in ifs. You can use this as a workaround:

if (!!bar.xyz == true) {
0
source

Your problem still exists as an open problem. https://github.com/facebook/flow/issues/106

, bar.xyz Boolean.

if (Boolean(bar.xyz))

.

10: if (Boolean (bar.xyz)) {                      ^ xyz.

10: if (Boolean (bar.xyz)) {                 ^

+1

Source: https://habr.com/ru/post/1689484/


All Articles