-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In typescript 3.9.0, the intersection between two objects will be `never` if the two objects have incompatible private members. This broke an implicit behavior that `CombineObjects` was taking advantage of; element-wise intersection. Because `CombineObjects` is intended to be a thin wrapper over the raw intersection type, I don't want its contract to deviate from the intersection. So instead I added the `ElementwiseIntersect` utility; which relies on the newly added `TryKey` utility. `TryKey` is just like `GetKey`, except it fails "silently" when the key does not exist. Specifically, `GetKey` returns `never` so that the resultant type is unusable and `TryKey` returns `unknown` which can be eliminated via intersection. Relevant PR from typescript which changed behavior of intersections (and broke the future-proofing test cases): microsoft/TypeScript#37762
- Loading branch information
Showing
3 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import test from 'ava'; | ||
import { assert } from '../helpers/assert'; | ||
|
||
import { ElementwiseIntersect } from '../../src'; | ||
|
||
test('Can combine two objects elementwise', t => { | ||
type a = { x: number, y: 'hi' }; | ||
type b = { z: number, y: 'there' }; | ||
|
||
type got = ElementwiseIntersect<a, b>; | ||
type expected = { | ||
x: number, | ||
y: 'hi' & 'there', | ||
z: number, | ||
}; | ||
|
||
assert<got, expected>(t); | ||
assert<expected, got>(t); | ||
}); | ||
|
||
test('Can combine two objects with private members elementwise', t => { | ||
class A { | ||
a: number = 1; | ||
private x: number = 2; | ||
y: 'hi' = 'hi'; | ||
private z: 'hey' = 'hey'; | ||
} | ||
|
||
class B { | ||
a: 22 = 22; | ||
private x: number = 2; | ||
y: 'there' = 'there'; | ||
private z: 'friend' = 'friend'; | ||
} | ||
|
||
type got = ElementwiseIntersect<A, B>; | ||
type expected = { | ||
a: 22, | ||
y: 'hi' & 'there', | ||
}; | ||
|
||
assert<got, expected>(t); | ||
assert<expected, got>(t); | ||
}); |