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

javascript - Flowtype array of object type does not allow extra properties - Stack Overflow

programmeradmin0浏览0评论

I am having a strange error with flow. I'm simply trying to have a function that accepts an array of objects with an amount property but I'm having errors when providing objects with more properties.

const sum = (items: Array<{amount: number}>) => {/* something */}

type Item = {
  amount: number,
  name: string
};

const list: Array<Item> = [];

sum(list);

This gives me the following errors:

10: const list: Array<Item> = [];
                      ^ property `name`. Property not found in
2: const sum = (items: Array<{amount: number}>) => {/* something */}
                             ^ object type

+AJTpBcBgHoAVAjDI8MABbYIAczjTJ-YLpg0ADnjgBJfKgwNgcOM1btOPPgBprcCMzwlY5VWuD8ANy64NDwADbYsCQU1PRmBOIYANoAusHASMiYkbDCgUA

I am having a strange error with flow. I'm simply trying to have a function that accepts an array of objects with an amount property but I'm having errors when providing objects with more properties.

const sum = (items: Array<{amount: number}>) => {/* something */}

type Item = {
  amount: number,
  name: string
};

const list: Array<Item> = [];

sum(list);

This gives me the following errors:

10: const list: Array<Item> = [];
                      ^ property `name`. Property not found in
2: const sum = (items: Array<{amount: number}>) => {/* something */}
                             ^ object type

https://flow/try/#0FAYw9gdgzgLgBFArgWzgXjgCgJYwKbJQBccAggE7kCGAngDwDeVyYiEMJEKARnuQL4A+AJTpBcBgHoAVAjDI8MABbYIAczjTJ-YLpg0ADnjgBJfKgwNgcOM1btOPPgBprcCMzwlY5VWuD8ANy64NDwADbYsCQU1PRmBOIYANoAusHASMiYkbDCgUA

Share Improve this question asked May 24, 2017 at 10:37 BalzardBalzard 1,2861 gold badge12 silver badges17 bronze badges 1
  • 1 I think it's bug that should be reported. If I replace it with $ReadOnlyArray<{amount: number}> it works… – Robin Pokorny Commented May 24, 2017 at 12:21
Add a ment  | 

1 Answer 1

Reset to default 8

Similar to the issue described here: https://github./facebook/flow/issues/1644

This is not allowed, because the declaration you have,

const sum = (items: Array<{amount: number}>) => {/* something */}

would allow

const sum = (items: Array<{amount: number}>) => {
    items.push({amount: 4});
};

which is totally valid given the declaration of items. The array passed to sum is a list of your Item type, which requires a name, which isn't set here.

As you mentioned, $ReadOnlyArray works, because it is read-only and can't have more items added to it. Alternatively you could drop the type definitions from sum entirely and let Flow infer them, or change Array<{amount: number}> to Array<*> so Flow knows it is an array, but will infer the type of the content. Another alternative (that you left in the ments) is

const sum = <T : {amount: number}>(items: Array<T>) => {/* something */}

which will set T to Item based on your call to sum(list), with the restriction that it will accept any T that is an object with a numeric amount property.

This is probably the best option.

发布评论

评论列表(0)

  1. 暂无评论