回调中的问号 [英] Question mark in callback

查看:39
本文介绍了回调中的问号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

static setItem(key: string, value: string, callback?: ?(error: ?Error) => void)

这是AsyncStorage中setitem的声明.第三个参数是回调.有人可以在这里解释一下问号的使用.我熟悉如何使用promise,但无法处理问号.

This is the declaration of setitem in AsyncStorage. the third parameter is a callback. Could some one explain the use of question marks here. I am familiar with how to use promise but couldn't get a handle of question mark.

推荐答案

AsyncStorage 使用 flow - Facebook 的开源静态类型检查器.你会在文件的开头找到@flow,它标记了启用流的源.Flow 对变量类型进行了大量检查(包括自动类型推断),但它还允许您指定变量和参数的类型.例如,在上面的示例中,'key: string' 表示 key 应该是字符串类型(它不是有效的 javascript 构造 - 您不能在 javascript 中指定类型).React 内置了转换器,可以将其转换为纯 javascript(因此所有类型都被剥离),但在此之前,流程会检查类型是否正确传递并找到诸如传递 null 或 undefined 之类的东西,然后将其用作对象和许多其他检查.您可以在 http://flowtype.org/ 中阅读规范.

AsyncStorage uses flow - Facebook's open-sourced static type checker. You will find @flow at the beginning of the file and it marks flow-enabled source. Flow does a lot of checking on the variable types (including automated type inference) but it also lets you specify the types for variables and parameters. In the example above 'key: string' for example indicates that key should be string type (it's not a valid javascript construct - you cannot specify type in javascript). React has built in transformers that transform it to pure javascript (so all the types are stripped) but before that flow checks if types are passed around properly and find things like passing null or undefined and using it later as object and many other checks. You can read the specs in http://flowtype.org/.

所以回答你的详细问号问题:

So answering your detailed questionmark question:

这篇关于回调中的问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆