- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
作为 JavaScript 开发人员,我是类型检查的新手,我很难理解为什么这个简单的代码不起作用:
type Animal = {
id: number,
name: string,
type: 'dog' | 'cat'
};
type Dog = {
id: number,
name: string,
type: 'dog',
color: string
};
function printAnimal(animal: Animal): string {
return `${animal.type}: ${animal.name}`;
}
const buddy: Dog = {
id: 1,
name: 'Buddy',
type: 'dog',
color: 'black'
}
printAnimal(buddy);
我在这里试图实现的是有一个接受接口(interface)的方法。然而,这给了我错误: Cannot call 'printAnimal' with 'buddy' bound to 'animal because string literal 'dog' [1] is incompatible with string literal 'cat' [2] in property 'type'.
.
我尝试过的:
interface Animal {//...}
- 不起作用。buddy
中删除输入 - 它有效,但我不满意。有时我确实想要更严格的类型(所以我知道我正在处理狗而不是猫)但仍然使用接受任何动物的通用方法。type: 'dog' | 'cat'
到 type: string
- 不起作用。我希望 'dog'
字符串是一般 string
类型的子类型,但事实并非如此。另一方面,即使它有效,也不够 - 有时我知道我的应用程序只接受狗和猫,不接受任何其他动物。感谢阅读,希望能得到大家的帮助!这是实时版本:Try Flow - live example
最佳答案
您必须使 Animal
类型成为一个接口(interface),因为它将您的类型实现描述为“父级”。如果您通过 联合 扩展您的 Dog
类型来强制执行它,那将是有意义的,因为这就是使用类型来实现更强大的类型检查的意义所在。
可以这样写:
/* @flow */
interface Animal {
id: number,
name: string,
type: 'dog' | 'cat'
};
type Dog = Animal & {
type: 'dog',
color: string
};
function printAnimal(animal: Animal): string {
return `${animal.type}: ${animal.name}`;
}
const buddy: Dog = {
id: 1,
name: 'Buddy',
type: 'dog',
color: 'black'
}
printAnimal(buddy);
关于javascript - Flowtype 扩展对象类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53260023/
我已经搜索了 flowtype 的文档,但找不到与类型推断相关的内容,例如: function add(x){ return x+10; } 使用flowtype后变成: function add
在以下示例中,由于我使用 switch 语句匹配消息类型,因此我希望流程能够识别我的错误大小写“ENUM_TYPO”。目前没有。 type Message = 'BROADCAST_MESSAGE'
我是spacemacs扇子。我想使用Facebook Flow但我不知道如何将它与 spacemacs 集成。我正在将 flow 与 nuclide 结合使用,但我需要重新学习一切才能提高工作效率。有
为了性能起见,我想忽略我的 .flowconfig [ignore] 部分中的整个 node_modules,但包括我使用的一些目录,例如“react”。 有人知道怎么做吗? 我基本上想忽略所有影响
安装在package.json上 "babel-preset-flow": "^6.23.0", "eslint-plugin-flowtype": "2.35.1", "flow-bin": "^0
想到一个简单的例子比如: class CommentAreaComponent extends React.Component { static propTypes = { id: PropT
我想确保我得到的值是一个十六进制字符串。目前我说 type Color = string; function foo(color: Color){} 但我想说 type Color = '#' + s
我不知道如何在两个文件中共享类型定义。考虑: // /file_a.js export const randomVariable: MySharedType = {thing: true}; // /
Flow 的文档说:When you create an object without any properties, you create an unsealed object type in Fl
免责声明:一般来说,我还是有点新的 Flow 和静态类型,所以我很可能忽略了这个问题的一些简单/明显的东西。 假设我有一个名为 my-library 的库.该库向其用户公开了一个模块,index.js
给定以下代码: /* @flow */ interface IDefaultSettings { Drivers?: {}, Options?: {} } const defaultSetti
我试图定义一个对象不能有某个键。 这是我的案例: alert({ items: [{ label:'Apple' }, { label:'Orange' }] }) alert({ ite
添加Flowtype是否可行现有的大型项目? 我添加了 /* @flow weak */到单个 .js文件,然后运行 flow check它突出了对全局定义库的大量函数调用,因为它不知道它们是什么
如何在流程中实现以下目标 export type Response = { err: string, data: ?Array, } | { data: Array, }; 我想表达一个类
我对flow check和flow的作用尚不完全清楚。运行flow似乎可以启动服务器,并检查所有代码。由于服务器正在运行,因此flow的后续执行速度更快。 flow check似乎也进行了完整的代码检
在 Flow 中,为什么要使用类而不是类型? type Point = {x: number; y: number}; class Point = {x: number; y: number}; 最佳
很多时候我发现自己作为 arg 传递了一个特定的 Shape 类型,但是每个键都是可选的,只有至少一个是必需的。 例如: type Shape = { +isFetching: boolean
我想知道如何避免这些大量的空检查,或者至少了解重点是什么,因为它似乎适得其反。 如果我省略空检查,Flowtype 会给我一个错误: var myEl = new MyElement() if (do
我有一个函数foo : function foo(a: string, b: string, c: number, d: boolean): Promise { return new Promis
给定默认值,如何定义config参数的类型? function (config = {}) {}; 最佳答案 function f(config: Object = {}) {} 或者,更一般而言:
我是一名优秀的程序员,十分优秀!