The thread does not complain about the wrong type

In this snippet of code, the thread does not complain that the value of dog set to state. It seems to be ignoring the definition of NamespaceData . I created types to complain. I am running through a nuclide and the thread is working correctly for everything else.

All action properties such as namespace , project , collection are strings.

 // @flow import { NAMESPACE_SET } from '../actions/NamespaceActions' type NamespaceData = { project: string, collection: string, } type NamespaceState = { [namespace: string]: NamespaceData, } const initialState: NamespaceState = {} function namespaceReducer(state: NamespaceState = initialState, action: Object): NamespaceState { switch (action) { case NAMESPACE_SET: { return { ...state, [action.namespace]: { project: action.project, collection: action.collection, dog: 1, } } } } return state } export default namespaceReducer 
+6
source share
1 answer

The stream is not strict regarding unknown properties of objects by default, for example

 // @flow type Thing = { known: string; }; var obj: Thing = { known: 'hi', unknown: 4, }; 

typechecks is great, even if unknown not type.

Stream 0.32 includes

  • New syntax for exact object types: use {| and |} instead of {and}. Where {x: string} contains at least the property x, {| x: string |} contains ONLY the x property.

In your example, you need the exact syntax of an object with:

 type NamespaceData = {| project: string, collection: string, |}; 
+7
source

Source: https://habr.com/ru/post/1012550/


All Articles