New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Record Flow type improvements #1870
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,11 +14,9 @@ const Point3: RecordFactory<{ x: number, y: number, z: number }> = Record({ | |
type TGeoPoint = { lat: ?number, lon: ?number }; | ||
const GeoPoint: RecordFactory<TGeoPoint> = Record({ lat: null, lon: null }); | ||
|
||
// TODO: this should be FlowExpectedError - 'abc' is not a number | ||
// However, due to support for the brittle support for subclassing, Flow | ||
// cannot also type check default values in this position. | ||
const PointWhoops: RecordFactory<{ x: number, y: number }> = Record({ | ||
x: 0, | ||
// $FlowExpectedError[incompatible-type-arg] | ||
y: 'abc', | ||
}); | ||
|
||
|
@@ -146,14 +144,15 @@ const mistakeNewInstance = new MakePointNew({ x: 'string' }); | |
// Note use of + for Read Only. | ||
type TPerson = { +name: string, +age: number }; | ||
const defaultValues: TPerson = { name: 'Aristotle', age: 2400 }; | ||
const PersonRecord = Record(defaultValues); | ||
const PersonRecord = Record<TPerson>(defaultValues); | ||
|
||
class Person extends PersonRecord<TPerson> { | ||
class Person extends PersonRecord { | ||
getName(): string { | ||
return this.get('name'); | ||
} | ||
|
||
setName(name: string): this & TPerson { | ||
// $FlowIssue[incompatible-return] | ||
return this.set('name', name); | ||
} | ||
} | ||
|
@@ -170,23 +169,6 @@ person.get('unknown'); | |
// $FlowExpectedError[prop-missing] | ||
person.set('unknown', 'Thales'); | ||
|
||
// Note: not <TPerson> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what the use case here is (and Edit: To be clear, the test right above this one is now |
||
class PersonWithoutTypes extends PersonRecord { | ||
getName(): string { | ||
return this.get('name'); | ||
} | ||
|
||
setName(name: string): this & TPerson { | ||
return this.set('name', name); | ||
} | ||
} | ||
|
||
const person2 = new PersonWithoutTypes(); | ||
|
||
person2.get('name'); | ||
// Note: no error | ||
person2.get('unknown'); | ||
|
||
|
||
// Functional Merge | ||
|
||
|
@@ -215,3 +197,31 @@ const record = xyRecord(); | |
(merge(record, Map([['z', 123]])): XYPointRecord); | ||
// $FlowExpectedError[incompatible-call] | ||
(merge(record, List([123])): XYPointRecord); | ||
|
||
|
||
// Type inference (issue #1722) | ||
|
||
type Product = { address: string }; | ||
type App = { name: string }; | ||
const AppDefaults: App = { name: '' }; | ||
|
||
const AppRecordImplicit = Record(AppDefaults); | ||
const AppRecordExplicit = Record<App>({ name: '' }); | ||
// $FlowExpectedError[prop-missing] | ||
const AppRecordBorked: RecordFactory<App> = Record<Product>({ address: '' }); | ||
|
||
const appImplicit = AppRecordImplicit({ name: 'i' }); | ||
(appImplicit.get('name'): string); | ||
(appImplicit.name: string); | ||
// $FlowExpectedError[incompatible-call] | ||
appImplicit.get('address'); | ||
// $FlowExpectedError[incompatible-use] | ||
appImplicit.address; | ||
|
||
const appExplicit = AppRecordExplicit({ name: 'e' }); | ||
(appExplicit.get('name'): string); | ||
(appExplicit.name: string); | ||
// $FlowExpectedError[incompatible-call] | ||
appExplicit.get('address'); | ||
// $FlowExpectedError[incompatible-use] | ||
appExplicit.address; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the new changes, Flow doesn't like the
this & TPerson
return type one bit: