BVB Source Codes

prettier Show test3.js Source code

Return Download prettier: download test3.js Source code - Download prettier Source code - Type:.js
  1. // @flow
  2.  
  3. /*
  4.    object literals are sealed. this is simply a heuristic
  5.    decision: most of the time, the rule gives the 'right'
  6.    errors.
  7.  
  8.    an exception is when a literal is used as an initializer
  9.    for an lvalue whose type specifies optional properties
  10.    missing from the literal, as below.
  11.  
  12.    the problem becomes visible when a property assignment
  13.    is then used to (legitimately) extend the object with an
  14.    optional property - the variable's specific (path-
  15.    dependent) type has become that of the literal which.
  16.    without adjustment, will reject the property addition.
  17.  
  18.    the solution in cases where a sealed object type (as from
  19.    an object literal) flows to an object type with optional
  20.    properties, is to have the sealed type acquire the optional
  21.    properties.
  22.  */
  23.  
  24. // x has optional property b.
  25. // (note that the initializer here does not play into
  26. // the problem, it's just a placeholder. initializers
  27. // do not narrow the types of annotated variables as do
  28. // subsequent assignments.)
  29. //
  30. var x: { a: number, b?: number } = { a: 0 };
  31.  
  32. // now assign an object literal lacking property b.
  33. // the literal's type is sealed and has only a at creation.
  34. // but it then flows, specific ~> general, to x's annotation
  35. // type. at that point, it acquires b as an optional property.
  36. //
  37. x = { a: 0 };
  38.  
  39. // ...which allows this assignment to take place.
  40. x.b = 1;
  41.  
  42. // T7810506
  43. class A {
  44.     x: { a: number, b?: string };
  45.     foo() {
  46.         // Something similar should happen here, but doesn't: the problem is
  47.         // made explicit by adding generics (see test3_failure.js introduced by
  48.         // D2747512). There is a race between writing b on the object literal
  49.         // type and adding b as an optional property to it, since in general we
  50.         // cannot guarantee that the flow from the object literal to the
  51.         // annotation will be processed before the flow involving the
  52.         // access. Here we lose the race and get an error on the write.
  53.         this.x = { a: 123 };
  54.         this.x.b = 'hello';
  55.     }
  56. }
  57.  
downloadtest3.js Source code - Download prettier Source code
Related Source Codes/Software:
storybook - 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
parse-server - Parse-compatible API server module for Node/Expres... 2017-06-07
inferno - An extremely fast, React-like JavaScript library f... 2017-06-08
guetzli - Perceptual JPEG encoder 2017-06-08
cs-video-courses - List of Computer Science courses with video lectur... 2017-06-08
interviews - Everything you need to know to get the job. 2017-06-08
prepack - Prepack is a partial evaluator for JavaScript. Pre... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top