Flow: how do i properly type functions with properties?

Created on 18 Nov 2016  路  8Comments  路  Source: facebook/flow

How do I type this code properly (without using ES6 classes)

function Cat(name) {
  this.name = name;
}

const staticProps = {
  legs: 4
};

const prototype = {
  meow: function() { return "meow" + this.name }
};

Object.assign(Cat, staticProps, {prototype: prototype});

// Cat.legs
//  => 4
// const kitty = new Cat('kitty');
// kitty.meow();
//  => "meowkitty"

I am currently getting the error

property `legs`. Property not found in statics of function
enhancement feature request

Most helpful comment

I see now, that making classes with ES6 is not the best experience.

All 8 comments

oops, think i found it.
https://flowtype.org/docs/quick-reference.html#callable-objects

will re-open if i'm still having trouble

Still having trouble. how do I do this?

That link is now broken with the doc update :(

In your example, you basically have a class. You can just use the ES6 class syntax for it. If for some reason, you can't use ES6 syntax, you can use flow comment to work around the problem.

https://flow.org/try/#0GYVwdgxgLglg9mABAYQIZQBRlQWwKYCUiA3gFCKJQAWMAzgHTb6IC8iTeA3KQL6mkQEtKImHoYEAAoAnOAAdarEuUQAbPAHNaALkQAWXtwFCRc2VDhQAnnLxKyFfHADuu0JFgIMRYoml4oEGkkACInZxDEAGpKGgYORD4eIwB5ACMAKzxoelRaWhgNMAw0KAAaUShxKVkFCuIzS0sbPF1Gi2tbHgIjAHoAKm1tUgATbNVUf0QICfzEAH1S5QoOXWFpGDANbgoxWAg1TR12EBw0vGkdxHDvNagNre4+ft7+QTBhFHRkWeOfvNoAB5FugAHxKEroXSoMBWHpAA

I see now, that making classes with ES6 is not the best experience.

https://flow.org/en/docs/types/functions/#toc-callable-objects

Example:

type OutputInterceptorType = {|
  <T>(routine: () => Promise<T> | T): Promise<T>,
  output: ''
|};

@goodmind This issue can be closed, as far as I can tell.

Original example actually works as is
https://flow.org/try/#0GYVwdgxgLglg9mABAYQIZQBRlQWwKYCUiA3gFCKJQAWMAzgHTb6IC8iTeA3KQL6mkQEtKImHoYEAAoAnOAAdarEuUQAbPAHNaALkQAWXtwFCRc2VDhQAnnLxKyFfHADuu0JFgIMRYohUVpPCgQaSQAIidnMMQAakoaBg4-Cj4eIwB5ACMAKzxoelRaWhgNMAwVNCgAGmTRKHEpWQUalWIzS0sbPF12i2tbPgIjSvp1LVIAegmKFgA+fWMwYUQAaxgoayUwPGcUdAwAcjWNqwOh0mPrekjvbimZ+Yi8F0urMKA

I tried all the versions and didn't found where it didn't work

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cubika picture cubika  路  3Comments

Beingbook picture Beingbook  路  3Comments

mjj2000 picture mjj2000  路  3Comments

john-gold picture john-gold  路  3Comments

ctrlplusb picture ctrlplusb  路  3Comments