Question mark in callback

后端 未结 1 1690
感动是毒
感动是毒 2021-01-03 07:19
static setItem(key: string, value: string, callback?: ?(error: ?Error) => void)

This is the declaration of setitem in AsyncStorage. the third pa

相关标签:
1条回答
  • 2021-01-03 08:01

    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:

    • '?Error' indicates that error parameter is a "Maybe" type - i.e. it CAN be null and flow will not complain if null or undefined is passed here elsewhere in the code callback (http://flowtype.org/docs/nullable-types.html#type-annotating-null)
    • 'callback?' indicates an optional parameter - so it might be skipped http://flowtype.org/docs/functions.html#function-based-type-annotations
    • '?(error...)' is another "Maybe" type - it simply indicates that the callback function might take one parameter ('error') or no parameters at all.
    0 讨论(0)
提交回复
热议问题