admin管理员组文章数量:1406937
Extensive googling and reading through Flow docs and examples didn't show any example of a very mon pattern in Javascript - having functions that return classes. A canonical example is Backbone:
var User = Backbone.Model.extend({
getFullName: function() {
return this.get('firstName') + ' ' + this.get('lastName');
}
});
var exampleUser = new User();
exampleUser.set('firstName', 'Johny'); //set() is a method from Backbone.Model
exampleUser.set('lastName', 'Something');
exampleUser.getFullName(); //method ing from User class
In JSDoc, I could annotate the class as follows, with some IDEs being able to figure out a decent autopletion:
/**
* @class User
* @augments Backbone.Model
*/
var User = Backbone.Model.extend(/**@lends User.prototype */{
getFullName: function() {...}
});
Is there any way how to properly annotate this pattern in Flow?
Extensive googling and reading through Flow docs and examples didn't show any example of a very mon pattern in Javascript - having functions that return classes. A canonical example is Backbone:
var User = Backbone.Model.extend({
getFullName: function() {
return this.get('firstName') + ' ' + this.get('lastName');
}
});
var exampleUser = new User();
exampleUser.set('firstName', 'Johny'); //set() is a method from Backbone.Model
exampleUser.set('lastName', 'Something');
exampleUser.getFullName(); //method ing from User class
In JSDoc, I could annotate the class as follows, with some IDEs being able to figure out a decent autopletion:
/**
* @class User
* @augments Backbone.Model
*/
var User = Backbone.Model.extend(/**@lends User.prototype */{
getFullName: function() {...}
});
Is there any way how to properly annotate this pattern in Flow?
Share asked Oct 8, 2015 at 18:59 Maros UrbanecMaros Urbanec 4534 silver badges12 bronze badges1 Answer
Reset to default 8/* @flow */
class Model {
get(name: string): any {}
set(name: string, value: any): void {}
}
function extend<T>(def: T): Class<Model & T> {
throw new Error('not implemented')
}
var User = extend({
getFullName: function() {
return this.get('firstname') + this.get('lastname')
}
})
var a = new User
a.get('firstname')
a.getFullName()
// a.notExisting give error
I use intersection type and generic type to express the pattern that 'Given a definition object type T
, return a Class
that is both Model
and T
'
This code piles under brew-shipped flow 0.11
Below is my personal idea on flow. I have to agree that flow docs is scarce. The best way to learn its feature is probably reading flow's React annotation and flow's source. Flow is based on a sophisticated type inference algorithm, which allows you type-check a program without annotation. So Flow is designed to make you not annotate, and so does its documentation. However, type inference is not so advanced to free one from annotating. You code is an example.
本文标签:
版权声明:本文标题:javascript - Flowtype - how to write declaration for class factories, such as Backbone Models? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745053701a2639806.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论