Flowtype - 如何为 class 工厂编写声明,例如 Backbone 模型?
Flowtype - how to write declaration for class factories, such as Backbone Models?
大量谷歌搜索和阅读 Flow 文档和示例并未显示 Javascript 中非常常见模式的任何示例 - 具有 return classes 的功能。一个典型的例子是 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 coming from User class
在 JSDoc 中,我可以如下注释 class,一些 IDE 能够找出一个不错的自动完成:
/**
* @class User
* @augments Backbone.Model
*/
var User = Backbone.Model.extend(/**@lends User.prototype */{
getFullName: function() {...}
});
有什么方法可以在 Flow 中正确注释此模式?
/* @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
我用intersection type and generic type来表达模式'给定一个定义对象类型T
,return一个Class
既是Model
和 T
'
此代码在 brew-shipped flow 0.11 下编译
以下是我个人对流量的看法。我必须同意流程文档很少。了解其功能的最佳方法可能是阅读 Flow 的 React annotation and flow's source. Flow 基于一种复杂的类型推断算法,它允许您在没有注释的情况下对程序进行类型检查。所以 Flow 的设计目的是让你不用注释,它的文档也是如此。然而,类型推断并没有先进到可以免于注解的地步。你的代码就是一个例子。
大量谷歌搜索和阅读 Flow 文档和示例并未显示 Javascript 中非常常见模式的任何示例 - 具有 return classes 的功能。一个典型的例子是 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 coming from User class
在 JSDoc 中,我可以如下注释 class,一些 IDE 能够找出一个不错的自动完成:
/**
* @class User
* @augments Backbone.Model
*/
var User = Backbone.Model.extend(/**@lends User.prototype */{
getFullName: function() {...}
});
有什么方法可以在 Flow 中正确注释此模式?
/* @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
我用intersection type and generic type来表达模式'给定一个定义对象类型T
,return一个Class
既是Model
和 T
'
此代码在 brew-shipped flow 0.11 下编译
以下是我个人对流量的看法。我必须同意流程文档很少。了解其功能的最佳方法可能是阅读 Flow 的 React annotation and flow's source. Flow 基于一种复杂的类型推断算法,它允许您在没有注释的情况下对程序进行类型检查。所以 Flow 的设计目的是让你不用注释,它的文档也是如此。然而,类型推断并没有先进到可以免于注解的地步。你的代码就是一个例子。