2017-02-22 00:59:19 +03:00
|
|
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
|
|
|
2017-03-22 03:38:28 +03:00
|
|
|
exports[`test.js 1`] = `
|
2016-12-23 22:31:38 +03:00
|
|
|
"var x: { } = { foo: 0 };
|
|
|
|
var y: { foo?: string } = x; // OK in TypeScript, not OK in Flow
|
|
|
|
|
2017-02-22 00:59:19 +03:00
|
|
|
var z: string = y.foo || \\"\\";
|
2016-12-23 22:31:38 +03:00
|
|
|
|
|
|
|
var o = { };
|
|
|
|
y = o; // OK; we know that narrowing could not have happened
|
|
|
|
o.foo = 0; // future widening is constrained
|
|
|
|
|
|
|
|
function bar(config: { foo?: number }) {}
|
|
|
|
bar({});
|
2017-02-22 00:59:19 +03:00
|
|
|
bar({foo: \\"\\"});
|
2016-12-23 22:31:38 +03:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2016-12-28 05:40:04 +03:00
|
|
|
var x: {} = { foo: 0 };
|
2017-01-26 22:57:43 +03:00
|
|
|
var y: { foo?: string } = x; // OK in TypeScript, not OK in Flow
|
2017-01-31 19:27:44 +03:00
|
|
|
|
2017-02-22 00:59:19 +03:00
|
|
|
var z: string = y.foo || \\"\\";
|
2017-01-10 05:49:06 +03:00
|
|
|
|
2016-12-23 22:31:38 +03:00
|
|
|
var o = {};
|
2017-01-26 22:57:43 +03:00
|
|
|
y = o; // OK; we know that narrowing could not have happened
|
|
|
|
o.foo = 0; // future widening is constrained
|
2017-01-31 19:27:44 +03:00
|
|
|
|
2017-01-24 20:37:01 +03:00
|
|
|
function bar(config: { foo?: number }) {}
|
2016-12-23 22:31:38 +03:00
|
|
|
bar({});
|
2017-02-22 00:59:19 +03:00
|
|
|
bar({ foo: \\"\\" });
|
2017-01-11 18:16:38 +03:00
|
|
|
"
|
2016-12-23 22:31:38 +03:00
|
|
|
`;
|
|
|
|
|
2017-03-22 03:38:28 +03:00
|
|
|
exports[`test2.js 1`] = `
|
2016-12-23 22:31:38 +03:00
|
|
|
"var a: { foo?: string } = {};
|
|
|
|
a.foo = undefined; // This is not an error
|
|
|
|
a.foo = null; // But this is an error
|
|
|
|
|
|
|
|
var b: { foo?: ?string } = {};
|
|
|
|
b.foo = undefined; // This is fine
|
|
|
|
b.foo = null; // Also fine
|
|
|
|
|
|
|
|
var c: { foo?: string } = { foo: undefined }; // This is not an error
|
|
|
|
var d: { foo?: string } = { foo: null }; // But this is an error
|
|
|
|
|
|
|
|
var e: { foo?: ?string } = { foo: undefined }; // This is fine
|
|
|
|
var f: { foo?: ?string } = { foo: null }; // Also fine
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
var a: { foo?: string } = {};
|
2017-01-26 22:57:43 +03:00
|
|
|
a.foo = undefined; // This is not an error
|
|
|
|
a.foo = null; // But this is an error
|
2017-01-31 19:27:44 +03:00
|
|
|
|
2016-12-23 22:31:38 +03:00
|
|
|
var b: { foo?: ?string } = {};
|
2017-01-26 22:57:43 +03:00
|
|
|
b.foo = undefined; // This is fine
|
|
|
|
b.foo = null; // Also fine
|
2017-01-31 19:27:44 +03:00
|
|
|
|
2017-01-26 22:57:43 +03:00
|
|
|
var c: { foo?: string } = { foo: undefined }; // This is not an error
|
|
|
|
var d: { foo?: string } = { foo: null }; // But this is an error
|
2017-01-31 19:27:44 +03:00
|
|
|
|
2017-01-26 22:57:43 +03:00
|
|
|
var e: { foo?: ?string } = { foo: undefined }; // This is fine
|
2017-01-11 18:16:38 +03:00
|
|
|
var f: { foo?: ?string } = { foo: null }; // Also fine
|
|
|
|
"
|
2016-12-23 22:31:38 +03:00
|
|
|
`;
|
|
|
|
|
2017-03-22 03:38:28 +03:00
|
|
|
exports[`test3.js 1`] = `
|
2016-12-23 22:31:38 +03:00
|
|
|
"// @flow
|
|
|
|
|
|
|
|
/*
|
|
|
|
object literals are sealed. this is simply a heuristic
|
2017-02-22 00:59:19 +03:00
|
|
|
decision: most of the time, the rule gives the 'right'
|
2016-12-23 22:31:38 +03:00
|
|
|
errors.
|
|
|
|
|
|
|
|
an exception is when a literal is used as an initializer
|
|
|
|
for an lvalue whose type specifies optional properties
|
|
|
|
missing from the literal, as below.
|
|
|
|
|
|
|
|
the problem becomes visible when a property assignment
|
|
|
|
is then used to (legitimately) extend the object with an
|
2017-02-22 00:59:19 +03:00
|
|
|
optional property - the variable's specific (path-
|
2016-12-23 22:31:38 +03:00
|
|
|
dependent) type has become that of the literal which.
|
|
|
|
without adjustment, will reject the property addition.
|
|
|
|
|
|
|
|
the solution in cases where a sealed object type (as from
|
|
|
|
an object literal) flows to an object type with optional
|
|
|
|
properties, is to have the sealed type acquire the optional
|
|
|
|
properties.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// x has optional property b.
|
|
|
|
// (note that the initializer here does not play into
|
2017-02-22 00:59:19 +03:00
|
|
|
// the problem, it's just a placeholder. initializers
|
2016-12-23 22:31:38 +03:00
|
|
|
// do not narrow the types of annotated variables as do
|
|
|
|
// subsequent assignments.)
|
|
|
|
//
|
|
|
|
var x: { a: number, b?: number } = { a: 0 };
|
|
|
|
|
|
|
|
// now assign an object literal lacking property b.
|
2017-02-22 00:59:19 +03:00
|
|
|
// the literal's type is sealed and has only a at creation.
|
|
|
|
// but it then flows, specific ~> general, to x's annotation
|
2016-12-23 22:31:38 +03:00
|
|
|
// type. at that point, it acquires b as an optional property.
|
|
|
|
//
|
|
|
|
x = { a: 0 };
|
|
|
|
|
|
|
|
// ...which allows this assignment to take place.
|
|
|
|
x.b = 1;
|
|
|
|
|
|
|
|
// T7810506
|
|
|
|
class A {
|
|
|
|
x: { a: number, b?: string };
|
|
|
|
foo() {
|
2017-02-22 00:59:19 +03:00
|
|
|
// Something similar should happen here, but doesn't: the problem is
|
2016-12-23 22:31:38 +03:00
|
|
|
// made explicit by adding generics (see test3_failure.js introduced by
|
|
|
|
// D2747512). There is a race between writing b on the object literal
|
|
|
|
// type and adding b as an optional property to it, since in general we
|
|
|
|
// cannot guarantee that the flow from the object literal to the
|
|
|
|
// annotation will be processed before the flow involving the
|
|
|
|
// access. Here we lose the race and get an error on the write.
|
|
|
|
this.x = { a: 123 };
|
2017-02-22 00:59:19 +03:00
|
|
|
this.x.b = 'hello';
|
2016-12-23 22:31:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
// @flow
|
2017-01-10 05:49:06 +03:00
|
|
|
|
2016-12-23 22:31:38 +03:00
|
|
|
/*
|
|
|
|
object literals are sealed. this is simply a heuristic
|
2017-02-22 00:59:19 +03:00
|
|
|
decision: most of the time, the rule gives the 'right'
|
2016-12-23 22:31:38 +03:00
|
|
|
errors.
|
|
|
|
|
|
|
|
an exception is when a literal is used as an initializer
|
|
|
|
for an lvalue whose type specifies optional properties
|
|
|
|
missing from the literal, as below.
|
|
|
|
|
|
|
|
the problem becomes visible when a property assignment
|
|
|
|
is then used to (legitimately) extend the object with an
|
2017-02-22 00:59:19 +03:00
|
|
|
optional property - the variable's specific (path-
|
2016-12-23 22:31:38 +03:00
|
|
|
dependent) type has become that of the literal which.
|
|
|
|
without adjustment, will reject the property addition.
|
|
|
|
|
|
|
|
the solution in cases where a sealed object type (as from
|
|
|
|
an object literal) flows to an object type with optional
|
|
|
|
properties, is to have the sealed type acquire the optional
|
|
|
|
properties.
|
|
|
|
*/
|
2017-01-10 05:49:06 +03:00
|
|
|
|
2016-12-23 22:31:38 +03:00
|
|
|
// x has optional property b.
|
|
|
|
// (note that the initializer here does not play into
|
2017-02-22 00:59:19 +03:00
|
|
|
// the problem, it's just a placeholder. initializers
|
2016-12-23 22:31:38 +03:00
|
|
|
// do not narrow the types of annotated variables as do
|
|
|
|
// subsequent assignments.)
|
|
|
|
//
|
2017-01-10 05:49:06 +03:00
|
|
|
var x: { a: number, b?: number } = { a: 0 };
|
|
|
|
|
2016-12-23 22:31:38 +03:00
|
|
|
// now assign an object literal lacking property b.
|
2017-02-22 00:59:19 +03:00
|
|
|
// the literal's type is sealed and has only a at creation.
|
|
|
|
// but it then flows, specific ~> general, to x's annotation
|
2016-12-23 22:31:38 +03:00
|
|
|
// type. at that point, it acquires b as an optional property.
|
|
|
|
//
|
2016-12-28 05:40:04 +03:00
|
|
|
x = { a: 0 };
|
2017-01-10 05:49:06 +03:00
|
|
|
|
|
|
|
// ...which allows this assignment to take place.
|
2016-12-28 05:40:04 +03:00
|
|
|
x.b = 1;
|
2017-01-10 05:49:06 +03:00
|
|
|
|
|
|
|
// T7810506
|
2016-12-23 22:31:38 +03:00
|
|
|
class A {
|
2017-01-05 01:26:44 +03:00
|
|
|
x: { a: number, b?: string };
|
2016-12-23 22:31:38 +03:00
|
|
|
foo() {
|
2017-02-22 00:59:19 +03:00
|
|
|
// Something similar should happen here, but doesn't: the problem is
|
2017-01-10 05:49:06 +03:00
|
|
|
// made explicit by adding generics (see test3_failure.js introduced by
|
|
|
|
// D2747512). There is a race between writing b on the object literal
|
|
|
|
// type and adding b as an optional property to it, since in general we
|
|
|
|
// cannot guarantee that the flow from the object literal to the
|
|
|
|
// annotation will be processed before the flow involving the
|
|
|
|
// access. Here we lose the race and get an error on the write.
|
2016-12-28 05:40:04 +03:00
|
|
|
this.x = { a: 123 };
|
2017-02-22 00:59:19 +03:00
|
|
|
this.x.b = \\"hello\\";
|
2016-12-23 22:31:38 +03:00
|
|
|
}
|
2017-01-11 18:16:38 +03:00
|
|
|
}
|
|
|
|
"
|
2016-12-23 22:31:38 +03:00
|
|
|
`;
|
|
|
|
|
2017-04-08 17:52:38 +03:00
|
|
|
exports[`test3_exact_annot.js 1`] = `
|
|
|
|
"/* The logic that allows ({}: {p?:T}), described in test3.js, should _not_ also
|
|
|
|
fire for exact annotations. */
|
|
|
|
|
|
|
|
const a: {| a: number |} = { a: 1 };
|
|
|
|
const b: { a: number, b?: number } = a; // error: property \`b\` not found
|
|
|
|
b.b = 0; // because subsequent writes would widen the exact object
|
|
|
|
(a.b: number); // error: property \`b\` not found
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
/* The logic that allows ({}: {p?:T}), described in test3.js, should _not_ also
|
|
|
|
fire for exact annotations. */
|
|
|
|
|
|
|
|
const a: {| a: number |} = { a: 1 };
|
|
|
|
const b: { a: number, b?: number } = a; // error: property \`b\` not found
|
|
|
|
b.b = 0; // because subsequent writes would widen the exact object
|
|
|
|
(a.b: number); // error: property \`b\` not found
|
|
|
|
"
|
|
|
|
`;
|
|
|
|
|
2017-03-22 03:38:28 +03:00
|
|
|
exports[`test3_failure.js 1`] = `
|
2016-12-23 22:31:38 +03:00
|
|
|
"// generalization of failure in test3.js
|
|
|
|
|
|
|
|
class A<O: {x: { a: number, b?: string}}> {
|
|
|
|
o: O;
|
|
|
|
foo() {
|
|
|
|
this.o.x = { a: 123 };
|
2017-02-22 00:59:19 +03:00
|
|
|
this.o.x.b = 'hello'; // this is a spurious error (see test3.js for details)
|
2016-12-23 22:31:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2016-12-27 21:29:31 +03:00
|
|
|
// generalization of failure in test3.js
|
2017-01-10 05:49:06 +03:00
|
|
|
|
2017-01-05 01:26:44 +03:00
|
|
|
class A<O: { x: { a: number, b?: string } }> {
|
2016-12-27 21:29:31 +03:00
|
|
|
o: O;
|
|
|
|
foo() {
|
2016-12-28 05:40:04 +03:00
|
|
|
this.o.x = { a: 123 };
|
2017-02-22 00:59:19 +03:00
|
|
|
this.o.x.b = \\"hello\\"; // this is a spurious error (see test3.js for details)
|
2016-12-27 21:29:31 +03:00
|
|
|
}
|
2017-01-11 18:16:38 +03:00
|
|
|
}
|
|
|
|
"
|
2016-12-23 22:31:38 +03:00
|
|
|
`;
|