最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - object literal. Inexact type is incompatible with exact type (no object spread) - Stack Overflow

programmeradmin1浏览0评论

Flow works correctly with exact types in the below case:

type Something={|a: string|};
const x1: Something = {a: '42'};        // Flow is happy
const x2: Something = {};               // Flow correctly detects problem
const x3: Something = {a: '42', b: 42}; // --------||---------

… however Flow also plains at the following:

type SomethingEmpty={||};
const x: SomethingEmpty = {}; 

Message is:

object literal. Inexact type is inpatible with exact type

This is not the same case as this one as no spread is used.

Tested with the latest 0.57.3.

Flow works correctly with exact types in the below case:

type Something={|a: string|};
const x1: Something = {a: '42'};        // Flow is happy
const x2: Something = {};               // Flow correctly detects problem
const x3: Something = {a: '42', b: 42}; // --------||---------

… however Flow also plains at the following:

type SomethingEmpty={||};
const x: SomethingEmpty = {}; 

Message is:

object literal. Inexact type is inpatible with exact type

This is not the same case as this one as no spread is used.

Tested with the latest 0.57.3.

Share Improve this question edited Oct 19, 2017 at 23:14 Marcus Junius Brutus asked Oct 19, 2017 at 22:59 Marcus Junius BrutusMarcus Junius Brutus 27.3k46 gold badges202 silver badges350 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

The Object literal without properties is inferred as an unsealed object type in Flow, this is to say you can add properties to such an object or deconstruct non-existing properties without errors being raised:

// inferred as...

const o = {}; // unsealed object type
const p = {bar: true} // sealed object type

const x = o.foo; // type checks
o.bar = true; // type checks

const y = p.foo; // type error
p.baz = true; // type error

Try

To type an empty Object literal as an exact type without properties you need to seal it explicitly:

type Empty = {||};
const o :Empty = Object.seal({}); // type checks

Try

发布评论

评论列表(0)

  1. 暂无评论