optional object key javascript


These two objects are equivalent at runtime: However, despite their equivalent runtime behavior, Flow checks them slightly differently. Object types try to match the syntax for objects in JavaScript as much as (Note that any is unsafe and ","line":4,"start":46,"end":47},{"type":"T_IDENTIFIER","context":"normal","value":"foo","line":4,"start":47,"end":50},{"type":"T_ASSIGN","context":"normal","value":"=","line":4,"start":51,"end":52},{"type":"T_TRUE","context":"normal","value":"true","line":4,"start":53,"end":57},{"type":"T_SEMICOLON","context":"normal","value":";","line":4,"start":57,"end":58},{"type":"Line","context":"comment","value":"// Works!

Sometimes Flow is able to figure out (with certainty) the type of a property ","line":4,"start":58,"end":59},{"type":"T_IDENTIFIER","context":"normal","value":"foo","line":4,"start":59,"end":62},{"type":"T_SEMICOLON","context":"normal","value":";","line":4,"start":62,"end":63}],"errors":[]}, {"value":"// @flow\nvar obj = {};\n\nif (Math.random()) obj.prop = true;\nelse obj.prop = \"hello\";\n\n// $ExpectError\nvar val1: boolean = obj.prop; // Error!\n// $ExpectError\nvar val2: string = obj.prop; // Error!\nvar val3: boolean | string = obj.prop; // Works!\n","tokens":[{"type":"Line","context":"comment","value":"// @flow","line":1,"start":0,"end":8},{"type":"T_VAR","context":"normal","value":"var","line":2,"start":9,"end":12},{"type":"T_IDENTIFIER","context":"normal","value":"obj","line":2,"start":13,"end":16},{"type":"T_ASSIGN","context":"normal","value":"=","line":2,"start":17,"end":18},{"type":"T_LCURLY","context":"normal","value":"{","line":2,"start":19,"end":20},{"type":"T_RCURLY","context":"normal","value":"}","line":2,"start":20,"end":21},{"type":"T_SEMICOLON","context":"normal","value":";","line":2,"start":21,"end":22},{"type":"T_IF","context":"normal","value":"if","line":4,"start":24,"end":26},{"type":"T_LPAREN","context":"normal","value":"(","line":4,"start":27,"end":28},{"type":"T_IDENTIFIER","context":"normal","value":"Math","line":4,"start":28,"end":32},{"type":"T_PERIOD","context":"normal","value":".","line":4,"start":32,"end":33},{"type":"T_IDENTIFIER","context":"normal","value":"random","line":4,"start":33,"end":39},{"type":"T_LPAREN","context":"normal","value":"(","line":4,"start":39,"end":40},{"type":"T_RPAREN","context":"normal","value":")","line":4,"start":40,"end":41},{"type":"T_RPAREN","context":"normal","value":")","line":4,"start":41,"end":42},{"type":"T_IDENTIFIER","context":"normal","value":"obj","line":4,"start":43,"end":46},{"type":"T_PERIOD","context":"normal","value":".","line":4,"start":46,"end":47},{"type":"T_IDENTIFIER","context":"normal","value":"prop","line":4,"start":47,"end":51},{"type":"T_ASSIGN","context":"normal","value":"=","line":4,"start":52,"end":53},{"type":"T_TRUE","context":"normal","value":"true","line":4,"start":54,"end":58},{"type":"T_SEMICOLON","context":"normal","value":";","line":4,"start":58,"end":59},{"type":"T_ELSE","context":"normal","value":"else","line":5,"start":60,"end":64},{"type":"T_IDENTIFIER","context":"normal","value":"obj","line":5,"start":65,"end":68},{"type":"T_PERIOD","context":"normal","value":". [prop-missing], // No type error, but will throw at runtime, Flow is planning to make object types exact by default.

[incompatible-type], Cannot call `acceptsObject` with object literal bound to `value` because null [1] is incompatible with string [2] in property `foo`. overview ","line":9,"start":141,"end":142},{"type":"T_IDENTIFIER","context":"normal","value":"prop","line":9,"start":142,"end":146},{"type":"T_SEMICOLON","context":"normal","value":";","line":9,"start":146,"end":147},{"type":"Line","context":"comment","value":"// Works! When you create an object without any properties, you create an unsealed ","line":4,"start":144,"end":145},{"type":"T_IDENTIFIER","context":"normal","value":"bop","line":4,"start":145,"end":148},{"type":"T_SEMICOLON","context":"normal","value":";","line":4,"start":148,"end":149},{"type":"Line","context":"comment","value":"// Works. ","line":5,"start":44,"end":45},{"type":"T_IDENTIFIER","context":"normal","value":"foo","line":5,"start":45,"end":48},{"type":"T_ASSIGN","context":"normal","value":"=","line":5,"start":49,"end":50},{"type":"T_LPAREN","context":"normal","value":"(","line":5,"start":51,"end":52},{"type":"T_RPAREN","context":"normal","value":")","line":5,"start":52,"end":53},{"type":"T_ARROW","context":"normal","value":"=>","line":5,"start":54,"end":56},{"type":"T_LCURLY","context":"normal","value":"{","line":5,"start":57,"end":58},{"type":"T_RETURN","context":"normal","value":"return","line":5,"start":59,"end":65},{"type":"T_NUMBER","context":"normal","value":"2","line":5,"start":66,"end":67},{"type":"T_SEMICOLON","context":"normal","value":";","line":5,"start":67,"end":68},{"type":"T_RCURLY","context":"normal","value":"}","line":5,"start":69,"end":70},{"type":"Line","context":"comment","value":"// Error! You might need the babel transform to use it. [incompatible-type]","context":"var val2: string = obj.prop; // Error! ","line":11,"start":177,"end":178},{"type":"T_IDENTIFIER","context":"normal","value":"baz","line":11,"start":178,"end":181},{"type":"T_SEMICOLON","context":"normal","value":";","line":11,"start":181,"end":182},{"type":"Line","context":"comment","value":"// Error!","line":11,"start":183,"end":192},{"type":"T_VAR","context":"normal","value":"var","line":12,"start":193,"end":196},{"type":"T_IDENTIFIER","context":"normal","value":"bat","line":12,"start":197,"end":200},{"type":"T_COLON","context":"type","value":":","line":12,"start":200,"end":201},{"type":"T_STRING_TYPE","context":"type","value":"string","line":12,"start":202,"end":208},{"type":"T_ASSIGN","context":"normal","value":"=","line":12,"start":210,"end":211},{"type":"T_IDENTIFIER","context":"normal","value":"obj","line":12,"start":212,"end":215},{"type":"T_PERIOD","context":"normal","value":". ","line":2,"start":43,"end":44},{"type":"T_IDENTIFIER","context":"normal","value":"length","line":2,"start":44,"end":50},{"type":"T_SEMICOLON","context":"normal","value":";","line":2,"start":50,"end":51},{"type":"Line","context":"comment","value":"// No type error, but will throw at runtime","line":2,"start":52,"end":95}],"errors":[]}, {"value":"// @flow\nvar obj: {\n size: number,\n [id: number]: string\n} = {\n size: 0\n};\n\nfunction add(id: number, name: string) {\n obj[id] = name;\n obj.size++;\n}\n","tokens":[{"type":"Line","context":"comment","value":"// @flow","line":1,"start":0,"end":8},{"type":"T_VAR","context":"normal","value":"var","line":2,"start":9,"end":12},{"type":"T_IDENTIFIER","context":"normal","value":"obj","line":2,"start":13,"end":16},{"type":"T_COLON","context":"type","value":":","line":2,"start":16,"end":17},{"type":"T_LCURLY","context":"type","value":"{","line":2,"start":18,"end":19},{"type":"T_IDENTIFIER","context":"normal","value":"size","line":3,"start":22,"end":26},{"type":"T_COLON","context":"type","value":":","line":3,"start":26,"end":27},{"type":"T_NUMBER_TYPE","context":"type","value":"number","line":3,"start":28,"end":34},{"type":"T_COMMA","context":"type","value":",","line":3,"start":34,"end":35},{"type":"T_LBRACKET","context":"type","value":"[","line":4,"start":38,"end":39},{"type":"T_IDENTIFIER","context":"type","value":"id","line":4,"start":39,"end":41},{"type":"T_COLON","context":"type","value":":","line":4,"start":41,"end":42},{"type":"T_NUMBER_TYPE","context":"type","value":"number","line":4,"start":43,"end":49},{"type":"T_RBRACKET","context":"type","value":"]","line":4,"start":49,"end":50},{"type":"T_COLON","context":"type","value":":","line":4,"start":50,"end":51},{"type":"T_STRING_TYPE","context":"type","value":"string","line":4,"start":52,"end":58},{"type":"T_RCURLY","context":"type","value":"}","line":5,"start":59,"end":60},{"type":"T_ASSIGN","context":"normal","value":"=","line":5,"start":61,"end":62},{"type":"T_LCURLY","context":"normal","value":"{","line":5,"start":63,"end":64},{"type":"T_IDENTIFIER","context":"normal","value":"size","line":6,"start":67,"end":71},{"type":"T_COLON","context":"normal","value":":","line":6,"start":71,"end":72},{"type":"T_NUMBER","context":"normal","value":"0","line":6,"start":73,"end":74},{"type":"T_RCURLY","context":"normal","value":"}","line":7,"start":75,"end":76},{"type":"T_SEMICOLON","context":"normal","value":";","line":7,"start":76,"end":77},{"type":"T_FUNCTION","context":"normal","value":"function","line":9,"start":79,"end":87},{"type":"T_IDENTIFIER","context":"normal","value":"add","line":9,"start":88,"end":91},{"type":"T_LPAREN","context":"normal","value":"(","line":9,"start":91,"end":92},{"type":"T_IDENTIFIER","context":"normal","value":"id","line":9,"start":92,"end":94},{"type":"T_COLON","context":"type","value":":","line":9,"start":94,"end":95},{"type":"T_NUMBER_TYPE","context":"type","value":"number","line":9,"start":96,"end":102},{"type":"T_COMMA","context":"normal","value":",","line":9,"start":102,"end":103},{"type":"T_IDENTIFIER","context":"normal","value":"name","line":9,"start":104,"end":108},{"type":"T_COLON","context":"type","value":":","line":9,"start":108,"end":109},{"type":"T_STRING_TYPE","context":"type","value":"string","line":9,"start":110,"end":116},{"type":"T_RPAREN","context":"normal","value":")","line":9,"start":116,"end":117},{"type":"T_LCURLY","context":"normal","value":"{","line":9,"start":118,"end":119},{"type":"T_IDENTIFIER","context":"normal","value":"obj","line":10,"start":122,"end":125},{"type":"T_LBRACKET","context":"normal","value":"[","line":10,"start":125,"end":126},{"type":"T_IDENTIFIER","context":"normal","value":"id","line":10,"start":126,"end":128},{"type":"T_RBRACKET","context":"normal","value":"]","line":10,"start":128,"end":129},{"type":"T_ASSIGN","context":"normal","value":"=","line":10,"start":130,"end":131},{"type":"T_IDENTIFIER","context":"normal","value":"name","line":10,"start":132,"end":136},{"type":"T_SEMICOLON","context":"normal","value":";","line":10,"start":136,"end":137},{"type":"T_IDENTIFIER","context":"normal","value":"obj","line":11,"start":140,"end":143},{"type":"T_PERIOD","context":"normal","value":". For safety, Flow restricts access to `this` inside object methods since these methods may be unbound and rebound. ","line":9,"start":129,"end":138},{"type":"Line","context":"comment","value":"// $ExpectError","line":10,"start":139,"end":154},{"type":"T_VAR","context":"normal","value":"var","line":11,"start":155,"end":158},{"type":"T_IDENTIFIER","context":"normal","value":"baz","line":11,"start":159,"end":162},{"type":"T_COLON","context":"type","value":":","line":11,"start":162,"end":163},{"type":"T_NULL","context":"type","value":"null","line":11,"start":164,"end":168},{"type":"T_ASSIGN","context":"normal","value":"=","line":11,"start":172,"end":173},{"type":"T_IDENTIFIER","context":"normal","value":"obj","line":11,"start":174,"end":177},{"type":"T_PERIOD","context":"normal","value":". [incompatible-type], Cannot assign `obj.bat` to `bat` because `void` (due to access of non-existent property `bat`) [1] is incompatible with string [2].

is missing in object literal [1]. ","line":4,"start":27,"end":28},{"type":"T_IDENTIFIER","context":"normal","value":"foo","line":4,"start":28,"end":31},{"type":"T_ASSIGN","context":"normal","value":"=","line":4,"start":32,"end":33},{"type":"T_NUMBER","context":"normal","value":"1","line":4,"start":34,"end":35},{"type":"T_SEMICOLON","context":"normal","value":";","line":4,"start":35,"end":36},{"type":"Line","context":"comment","value":"// Works!","line":4,"start":43,"end":52},{"type":"T_IDENTIFIER","context":"normal","value":"obj","line":5,"start":53,"end":56},{"type":"T_PERIOD","context":"normal","value":". ","line":8,"start":125,"end":126},{"type":"T_IDENTIFIER","context":"normal","value":"prop","line":8,"start":126,"end":130},{"type":"T_SEMICOLON","context":"normal","value":";","line":8,"start":130,"end":131},{"type":"Line","context":"comment","value":"// Error! In that case, Flow will give it the known type. ","line":6,"start":91,"end":92},{"type":"T_IDENTIFIER","context":"normal","value":"foo","line":6,"start":92,"end":95},{"type":"T_ASSIGN","context":"normal","value":"=","line":6,"start":96,"end":97},{"type":"T_STRING","context":"normal","value":"'hello'","line":6,"start":98,"end":105},{"type":"T_SEMICOLON","context":"normal","value":";","line":6,"start":105,"end":106},{"type":"Line","context":"comment","value":"// Error! object by using an ellipsis at the end of your property list: Flow is planning to make object types exact by default. type in Flow. Let us know by sending a message to Similar to var and let variables still very common to use objects as maps as well. indexer property. An indexer property allows reads and writes using any key ","line":9,"start":114,"end":123}],"errors":[{"id":"E1","messages":[{"id":"E1M1","description":"Cannot assign `true` to `obj.bar` because property `bar` is missing in object literal [1].

future. ","source":"-","start":{"line":9,"column":5,"offset":99},"end":{"line":9,"column":7,"offset":102}}],"operation":null}]}, {"value":"// @flow\nvar obj = {};\n\nobj.foo = 1; // Works!\nobj.bar = true; // Works!\nobj.baz = 'three'; // Works!\n","tokens":[{"type":"Line","context":"comment","value":"// @flow","line":1,"start":0,"end":8},{"type":"T_VAR","context":"normal","value":"var","line":2,"start":9,"end":12},{"type":"T_IDENTIFIER","context":"normal","value":"obj","line":2,"start":13,"end":16},{"type":"T_ASSIGN","context":"normal","value":"=","line":2,"start":17,"end":18},{"type":"T_LCURLY","context":"normal","value":"{","line":2,"start":19,"end":20},{"type":"T_RCURLY","context":"normal","value":"}","line":2,"start":20,"end":21},{"type":"T_SEMICOLON","context":"normal","value":";","line":2,"start":21,"end":22},{"type":"T_IDENTIFIER","context":"normal","value":"obj","line":4,"start":24,"end":27},{"type":"T_PERIOD","context":"normal","value":". before reads (in the order of execution). To indicate optional key, you can assign to it null, if the condition is false, Open Bluetooth settings programmatically from UIAlertController, Passing release variables between two agent phases. ","line":2,"start":71,"end":80}],"errors":[{"id":"E1","messages":[{"id":"E1M1","description":"Cannot assign object literal to `foo` because property `bar` is missing in object type [1] but exists in object literal [2]. Using curly braces {} and name-value pairs using a colon : split Consider replacing the reference to `this` with the name of the object, or rewriting the object as a class. undefined. ","source":"-","start":{"line":9,"column":22,"offset":198},"end":{"line":9,"column":25,"offset":202}}],"operation":null}]}, {"value":"// @flow\nlet a = {\n foo : function () { return 3; }\n};\nlet b = {\n foo() { return 3; }\n}\n","tokens":[{"type":"Line","context":"comment","value":"// @flow","line":1,"start":0,"end":8},{"type":"T_LET","context":"normal","value":"let","line":2,"start":9,"end":12},{"type":"T_IDENTIFIER","context":"normal","value":"a","line":2,"start":13,"end":14},{"type":"T_ASSIGN","context":"normal","value":"=","line":2,"start":15,"end":16},{"type":"T_LCURLY","context":"normal","value":"{","line":2,"start":17,"end":18},{"type":"T_IDENTIFIER","context":"normal","value":"foo","line":3,"start":21,"end":24},{"type":"T_COLON","context":"normal","value":":","line":3,"start":25,"end":26},{"type":"T_FUNCTION","context":"normal","value":"function","line":3,"start":27,"end":35},{"type":"T_LPAREN","context":"normal","value":"(","line":3,"start":36,"end":37},{"type":"T_RPAREN","context":"normal","value":")","line":3,"start":37,"end":38},{"type":"T_LCURLY","context":"normal","value":"{","line":3,"start":39,"end":40},{"type":"T_RETURN","context":"normal","value":"return","line":3,"start":41,"end":47},{"type":"T_NUMBER","context":"normal","value":"3","line":3,"start":48,"end":49},{"type":"T_SEMICOLON","context":"normal","value":";","line":3,"start":49,"end":50},{"type":"T_RCURLY","context":"normal","value":"}","line":3,"start":51,"end":52},{"type":"T_RCURLY","context":"normal","value":"}","line":4,"start":53,"end":54},{"type":"T_SEMICOLON","context":"normal","value":";","line":4,"start":54,"end":55},{"type":"T_LET","context":"normal","value":"let","line":5,"start":56,"end":59},{"type":"T_IDENTIFIER","context":"normal","value":"b","line":5,"start":60,"end":61},{"type":"T_ASSIGN","context":"normal","value":"=","line":5,"start":62,"end":63},{"type":"T_LCURLY","context":"normal","value":"{","line":5,"start":64,"end":65},{"type":"T_IDENTIFIER","context":"normal","value":"foo","line":6,"start":68,"end":71},{"type":"T_LPAREN","context":"normal","value":"(","line":6,"start":71,"end":72},{"type":"T_RPAREN","context":"normal","value":")","line":6,"start":72,"end":73},{"type":"T_LCURLY","context":"normal","value":"{","line":6,"start":74,"end":75},{"type":"T_RETURN","context":"normal","value":"return","line":6,"start":76,"end":82},{"type":"T_NUMBER","context":"normal","value":"3","line":6,"start":83,"end":84},{"type":"T_SEMICOLON","context":"normal","value":";","line":6,"start":84,"end":85},{"type":"T_RCURLY","context":"normal","value":"}","line":6,"start":86,"end":87},{"type":"T_RCURLY","context":"normal","value":"}","line":7,"start":88,"end":89}],"errors":[]}, {"value":"// @flow\nlet b = {\n foo() { return 3; }\n}\nb.foo = () => { return 2; } // Error!\n","tokens":[{"type":"Line","context":"comment","value":"// @flow","line":1,"start":0,"end":8},{"type":"T_LET","context":"normal","value":"let","line":2,"start":9,"end":12},{"type":"T_IDENTIFIER","context":"normal","value":"b","line":2,"start":13,"end":14},{"type":"T_ASSIGN","context":"normal","value":"=","line":2,"start":15,"end":16},{"type":"T_LCURLY","context":"normal","value":"{","line":2,"start":17,"end":18},{"type":"T_IDENTIFIER","context":"normal","value":"foo","line":3,"start":21,"end":24},{"type":"T_LPAREN","context":"normal","value":"(","line":3,"start":24,"end":25},{"type":"T_RPAREN","context":"normal","value":")","line":3,"start":25,"end":26},{"type":"T_LCURLY","context":"normal","value":"{","line":3,"start":27,"end":28},{"type":"T_RETURN","context":"normal","value":"return","line":3,"start":29,"end":35},{"type":"T_NUMBER","context":"normal","value":"3","line":3,"start":36,"end":37},{"type":"T_SEMICOLON","context":"normal","value":";","line":3,"start":37,"end":38},{"type":"T_RCURLY","context":"normal","value":"}","line":3,"start":39,"end":40},{"type":"T_RCURLY","context":"normal","value":"}","line":4,"start":41,"end":42},{"type":"T_IDENTIFIER","context":"normal","value":"b","line":5,"start":43,"end":44},{"type":"T_PERIOD","context":"normal","value":".

","line":9,"start":148,"end":157}],"errors":[{"id":"E1","messages":[{"id":"E1M1","description":"Cannot assign `obj.prop` to `val1` because string [1] is incompatible with boolean [2].

[prop-missing]","context":"const fooBarFail: FooBarFailT = { foo: '123', bar: 12 }; // Error!

However, they cannot be null.

precise and interact better with other type system features, like spreads.

flutter dart cheatsheet different ways to type them in order to support all the different use cases. ","line":6,"start":107,"end":116}],"errors":[{"id":"E1","messages":[{"id":"E1M1","description":"Cannot assign `'hello'` to `obj.foo` because string [1] is incompatible with boolean [2]. Intersections of exact object types may not work as you expect. ","line":8,"start":104,"end":105},{"type":"T_IDENTIFIER","context":"normal","value":"x","line":8,"start":105,"end":106},{"type":"T_SEMICOLON","context":"normal","value":";","line":8,"start":106,"end":107},{"type":"T_RCURLY","context":"normal","value":"}","line":8,"start":108,"end":109},{"type":"Line","context":"comment","value":"// works!

that reads are compatible with writes, but does not ensure that writes happen This is available via an option in your flowconfig. after the property name in ","source":"-","start":{"line":9,"column":33,"offset":177},"end":{"line":9,"column":55,"offset":200}}],"operation":null},{"id":"E2","messages":[{"id":"E2M1","description":"Cannot assign object literal to `fooBarFail` because property `foo` is missing in `BarT` [1] but exists in object literal [2]. In Flow, it is considered safe to pass an object with extra properties where Furthermore, the property keys may not even be known statically, so writing out [incompatible-type], Cannot assign object literal to `foo` because property `bar` is missing in object type [1] but exists in object literal [2]. [incompatible-call]","context":"acceptsObject({ foo: null }); // Error!

them with and the types of their values. For objects like these, Flow provides a special kind of property, called an

[incompatible-type]","context":"var val1: boolean = obj.prop; // Error!

","line":10,"start":181,"end":182},{"type":"T_IDENTIFIER","context":"normal","value":"prop","line":10,"start":182,"end":186},{"type":"T_SEMICOLON","context":"normal","value":";","line":10,"start":186,"end":187},{"type":"Line","context":"comment","value":"// Error!","line":10,"start":188,"end":197},{"type":"T_VAR","context":"normal","value":"var","line":11,"start":198,"end":201},{"type":"T_IDENTIFIER","context":"normal","value":"val3","line":11,"start":202,"end":206},{"type":"T_COLON","context":"type","value":":","line":11,"start":206,"end":207},{"type":"T_BOOLEAN_TYPE","context":"type","value":"boolean","line":11,"start":208,"end":215},{"type":"T_BIT_OR","context":"type","value":"|","line":11,"start":216,"end":217},{"type":"T_STRING_TYPE","context":"type","value":"string","line":11,"start":218,"end":224},{"type":"T_ASSIGN","context":"normal","value":"=","line":11,"start":225,"end":226},{"type":"T_IDENTIFIER","context":"normal","value":"obj","line":11,"start":227,"end":230},{"type":"T_PERIOD","context":"normal","value":". ","line":5,"start":44,"end":45},{"type":"T_IDENTIFIER","context":"normal","value":"prop","line":5,"start":45,"end":49},{"type":"T_ASSIGN","context":"normal","value":"=","line":5,"start":50,"end":51},{"type":"T_STRING","context":"normal","value":"\"hello\"","line":5,"start":52,"end":59},{"type":"T_SEMICOLON","context":"normal","value":";","line":5,"start":59,"end":60},{"type":"Line","context":"comment","value":"// $ExpectError","line":7,"start":62,"end":77},{"type":"T_VAR","context":"normal","value":"var","line":8,"start":78,"end":81},{"type":"T_IDENTIFIER","context":"normal","value":"val1","line":8,"start":82,"end":86},{"type":"T_COLON","context":"type","value":":","line":8,"start":86,"end":87},{"type":"T_BOOLEAN_TYPE","context":"type","value":"boolean","line":8,"start":88,"end":95},{"type":"T_ASSIGN","context":"normal","value":"=","line":8,"start":96,"end":97},{"type":"T_IDENTIFIER","context":"normal","value":"obj","line":8,"start":98,"end":101},{"type":"T_PERIOD","context":"normal","value":".

","line":7,"start":105,"end":106},{"type":"T_IDENTIFIER","context":"normal","value":"bar","line":7,"start":106,"end":109},{"type":"T_SEMICOLON","context":"normal","value":";","line":7,"start":109,"end":110},{"type":"Line","context":"comment","value":"// Works!","line":7,"start":111,"end":120},{"type":"T_VAR","context":"normal","value":"var","line":8,"start":121,"end":124},{"type":"T_IDENTIFIER","context":"normal","value":"baz","line":8,"start":125,"end":128},{"type":"T_COLON","context":"type","value":":","line":8,"start":128,"end":129},{"type":"T_STRING_TYPE","context":"type","value":"string","line":8,"start":130,"end":136},{"type":"T_ASSIGN","context":"normal","value":"=","line":8,"start":138,"end":139},{"type":"T_IDENTIFIER","context":"normal","value":"obj","line":8,"start":140,"end":143},{"type":"T_PERIOD","context":"normal","value":". Note: Object Rest/Spread is a stage 4 proposal for ECMAScript. [prop-missing]","context":"obj.bar = true; // Error! ","source":"-","start":{"line":7,"column":5,"offset":54},"end":{"line":7,"column":7,"offset":57}}],"operation":null},{"id":"E2","messages":[{"id":"E2M1","description":"Cannot assign `'three'` to `obj.baz` because property `baz` is missing in object literal [1].

","line":5,"start":71,"end":80}],"errors":[{"id":"E1","messages":[{"id":"E1M1","description":"Cannot assign function to `b.foo` because property `foo` is not writable. [incompatible-type]","context":"var baz: null = obj.baz; // Error! In JavaScript, accessing a property that doesnt exist evaluates to You can use object spread to have an optional property. @flowtype. ","line":9,"start":212,"end":221},{"type":"T_IDENTIFIER","context":"normal","value":"acceptsObject","line":10,"start":222,"end":235},{"type":"T_LPAREN","context":"normal","value":"(","line":10,"start":235,"end":236},{"type":"T_LCURLY","context":"normal","value":"{","line":10,"start":236,"end":237},{"type":"T_RCURLY","context":"normal","value":"}","line":10,"start":237,"end":238},{"type":"T_RPAREN","context":"normal","value":")","line":10,"start":238,"end":239},{"type":"T_SEMICOLON","context":"normal","value":";","line":10,"start":239,"end":240},{"type":"Line","context":"comment","value":"// Works! ","source":"-","start":{"line":11,"column":20,"offset":174},"end":{"line":11,"column":26,"offset":181}}],"operation":null},{"id":"E2","messages":[{"id":"E2M1","description":"Cannot assign `obj.bat` to `bat` because `void` (due to access of non-existent property `bat`) [1] is incompatible with string [2]. ","source":"-","start":{"line":4,"column":5,"offset":55},"end":{"line":4,"column":7,"offset":58}}],"operation":null}]}, {"value":"// @flow\nvar obj: { foo? [incompatible-type]","context":"var val1: boolean = obj.prop; // Error! ","line":4,"start":157,"end":166},{"type":"T_RCURLY","context":"normal","value":"}","line":5,"start":167,"end":168},{"type":"T_IDENTIFIER","context":"normal","value":"method","line":7,"start":170,"end":176},{"type":"T_LPAREN","context":"normal","value":"(","line":7,"start":176,"end":177},{"type":"T_LCURLY","context":"normal","value":"{","line":7,"start":177,"end":178},{"type":"T_IDENTIFIER","context":"normal","value":"baz","line":7,"start":179,"end":182},{"type":"T_COLON","context":"normal","value":":","line":7,"start":182,"end":183},{"type":"T_NUMBER","context":"normal","value":"3.14","line":7,"start":184,"end":188},{"type":"T_COMMA","context":"normal","value":",","line":7,"start":188,"end":189},{"type":"T_IDENTIFIER","context":"normal","value":"bar","line":7,"start":190,"end":193},{"type":"T_COLON","context":"normal","value":":","line":7,"start":193,"end":194},{"type":"T_STRING","context":"normal","value":"\"hello\"","line":7,"start":195,"end":202},{"type":"T_RCURLY","context":"normal","value":"}","line":7,"start":203,"end":204},{"type":"T_RPAREN","context":"normal","value":")","line":7,"start":204,"end":205},{"type":"T_SEMICOLON","context":"normal","value":";","line":7,"start":205,"end":206}],"errors":[]}, {"value":"function method(obj: Object) {\n obj = 10;\n}\n\nmethod({ baz: 3.14, bar: \"hello\" });\n","tokens":[{"type":"T_FUNCTION","context":"normal","value":"function","line":1,"start":0,"end":8},{"type":"T_IDENTIFIER","context":"normal","value":"method","line":1,"start":9,"end":15},{"type":"T_LPAREN","context":"normal","value":"(","line":1,"start":15,"end":16},{"type":"T_IDENTIFIER","context":"normal","value":"obj","line":1,"start":16,"end":19},{"type":"T_COLON","context":"type","value":":","line":1,"start":19,"end":20},{"type":"T_IDENTIFIER","context":"type","value":"Object","line":1,"start":21,"end":27},{"type":"T_RPAREN","context":"normal","value":")","line":1,"start":27,"end":28},{"type":"T_LCURLY","context":"normal","value":"{","line":1,"start":29,"end":30},{"type":"T_IDENTIFIER","context":"normal","value":"obj","line":2,"start":33,"end":36},{"type":"T_ASSIGN","context":"normal","value":"=","line":2,"start":37,"end":38},{"type":"T_NUMBER","context":"normal","value":"10","line":2,"start":39,"end":41},{"type":"T_SEMICOLON","context":"normal","value":";","line":2,"start":41,"end":42},{"type":"T_RCURLY","context":"normal","value":"}","line":3,"start":43,"end":44},{"type":"T_IDENTIFIER","context":"normal","value":"method","line":5,"start":46,"end":52},{"type":"T_LPAREN","context":"normal","value":"(","line":5,"start":52,"end":53},{"type":"T_LCURLY","context":"normal","value":"{","line":5,"start":53,"end":54},{"type":"T_IDENTIFIER","context":"normal","value":"baz","line":5,"start":55,"end":58},{"type":"T_COLON","context":"normal","value":":","line":5,"start":58,"end":59},{"type":"T_NUMBER","context":"normal","value":"3.14","line":5,"start":60,"end":64},{"type":"T_COMMA","context":"normal","value":",","line":5,"start":64,"end":65},{"type":"T_IDENTIFIER","context":"normal","value":"bar","line":5,"start":66,"end":69},{"type":"T_COLON","context":"normal","value":":","line":5,"start":69,"end":70},{"type":"T_STRING","context":"normal","value":"\"hello\"","line":5,"start":71,"end":78},{"type":"T_RCURLY","context":"normal","value":"}","line":5,"start":79,"end":80},{"type":"T_RPAREN","context":"normal","value":")","line":5,"start":80,"end":81},{"type":"T_SEMICOLON","context":"normal","value":";","line":5,"start":81,"end":82}],"errors":[]}, Cannot get `obj.bar` because property `bar` is missing in object literal [1].