-
Notifications
You must be signed in to change notification settings - Fork 12.9k
add types for set methods proposal #57230
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b1ea83a
add types for set methods proposal
bakkot d9efe96
run/commit all baselines
bakkot 82378a9
update types
bakkot 6239762
update baselines
bakkot 6ceb395
fix description of symmetricDifference
bakkot 9e5aca4
fix type for keys
bakkot 6754d3b
Merge branch 'main' into set-methods
bakkot b523126
update baseline
bakkot 41aea33
add new methods to ReadonlySet
bakkot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -9,3 +9,80 @@ interface MapConstructor { | |
keySelector: (item: T, index: number) => K, | ||
): Map<K, T[]>; | ||
} | ||
|
||
interface ReadonlySetLike<T> { | ||
/** | ||
* Despite its name, returns an iterator of the values in the set-like. | ||
*/ | ||
keys(): Iterator<T>; | ||
/** | ||
* @returns a boolean indicating whether an element with the specified value exists in the set-like or not. | ||
*/ | ||
has(value: T): boolean; | ||
/** | ||
* @returns the number of (unique) elements in the set-like. | ||
*/ | ||
readonly size: number; | ||
} | ||
|
||
interface Set<T> { | ||
/** | ||
* @returns a new Set containing all the elements in this Set and also all the elements in the argument. | ||
*/ | ||
union<U>(other: ReadonlySetLike<U>): Set<T | U>; | ||
/** | ||
* @returns a new Set containing all the elements which are both in this Set and in the argument. | ||
*/ | ||
intersection<U>(other: ReadonlySetLike<U>): Set<T & U>; | ||
/** | ||
* @returns a new Set containing all the elements in this Set which are not also in the argument. | ||
*/ | ||
difference<U>(other: ReadonlySetLike<U>): Set<T>; | ||
/** | ||
* @returns a new Set containing all the elements which are in either this Set or in the argument, but not in both. | ||
*/ | ||
symmetricDifference<U>(other: ReadonlySetLike<U>): Set<T | U>; | ||
/** | ||
* @returns a boolean indicating whether all the elements in this Set are also in the argument. | ||
*/ | ||
isSubsetOf(other: ReadonlySetLike<unknown>): boolean; | ||
/** | ||
* @returns a boolean indicating whether all the elements in the argument are also in this Set. | ||
*/ | ||
isSupersetOf(other: ReadonlySetLike<unknown>): boolean; | ||
/** | ||
* @returns a boolean indicating whether this Set has no elements in common with the argument. | ||
*/ | ||
isDisjointFrom(other: ReadonlySetLike<unknown>): boolean; | ||
} | ||
|
||
interface ReadonlySet<T> { | ||
/** | ||
* @returns a new Set containing all the elements in this Set and also all the elements in the argument. | ||
*/ | ||
union<U>(other: ReadonlySetLike<U>): Set<T | U>; | ||
/** | ||
* @returns a new Set containing all the elements which are both in this Set and in the argument. | ||
*/ | ||
intersection<U>(other: ReadonlySetLike<U>): Set<T & U>; | ||
/** | ||
* @returns a new Set containing all the elements in this Set which are not also in the argument. | ||
*/ | ||
difference<U>(other: ReadonlySetLike<U>): Set<T>; | ||
/** | ||
* @returns a new Set containing all the elements which are in either this Set or in the argument, but not in both. | ||
*/ | ||
symmetricDifference<U>(other: ReadonlySetLike<U>): Set<T | U>; | ||
/** | ||
* @returns a boolean indicating whether all the elements in this Set are also in the argument. | ||
*/ | ||
isSubsetOf(other: ReadonlySetLike<unknown>): boolean; | ||
/** | ||
* @returns a boolean indicating whether all the elements in the argument are also in this Set. | ||
*/ | ||
isSupersetOf(other: ReadonlySetLike<unknown>): boolean; | ||
Comment on lines
+79
to
+83
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. @DanielRosenwasser would overloads like the following be worthwhile? interface Set<T> {
isSubsetOf<U>(other: ReadonlySetLike<U>): this is Set<T & U>;
isSupersetOf<U>(other: ReadonlySetLike<U>): other is ReadonlySetLike<T & U>;
} 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. We can always change these in a follow-up PR. |
||
/** | ||
* @returns a boolean indicating whether this Set has no elements in common with the argument. | ||
*/ | ||
isDisjointFrom(other: ReadonlySetLike<unknown>): boolean; | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,80 @@ | ||
setMethods.ts(13,17): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'. | ||
Type 'undefined[]' is missing the following properties from type 'ReadonlySetLike<unknown>': has, size | ||
setMethods.ts(19,24): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'. | ||
setMethods.ts(25,22): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'. | ||
setMethods.ts(31,31): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'. | ||
setMethods.ts(37,22): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'. | ||
setMethods.ts(43,24): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'. | ||
setMethods.ts(49,26): error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'. | ||
|
||
|
||
==== setMethods.ts (7 errors) ==== | ||
let numberSet = new Set([0, 1, 2]); | ||
|
||
let stringSet = new Set(["a", "b"]); | ||
|
||
let numberMap = new Map([[4, {}], [5, {}]]); | ||
|
||
let numberSetLike = { | ||
size: 1, | ||
*keys() { yield 3 }, | ||
has(x) { return x === 3 }, | ||
}; | ||
|
||
numberSet.union([]); | ||
~~ | ||
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'. | ||
!!! error TS2345: Type 'undefined[]' is missing the following properties from type 'ReadonlySetLike<unknown>': has, size | ||
numberSet.union(new Set); | ||
numberSet.union(stringSet); | ||
numberSet.union(numberMap); | ||
numberSet.union(numberSetLike); | ||
|
||
numberSet.intersection([]); | ||
~~ | ||
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'. | ||
numberSet.intersection(new Set); | ||
numberSet.intersection(stringSet); | ||
numberSet.intersection(numberMap); | ||
numberSet.intersection(numberSetLike); | ||
|
||
numberSet.difference([]); | ||
~~ | ||
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'. | ||
numberSet.difference(new Set); | ||
numberSet.difference(stringSet); | ||
numberSet.difference(numberMap); | ||
numberSet.difference(numberSetLike); | ||
|
||
numberSet.symmetricDifference([]); | ||
~~ | ||
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'. | ||
numberSet.symmetricDifference(new Set); | ||
numberSet.symmetricDifference(stringSet); | ||
numberSet.symmetricDifference(numberMap); | ||
numberSet.symmetricDifference(numberSetLike); | ||
|
||
numberSet.isSubsetOf([]); | ||
~~ | ||
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'. | ||
numberSet.isSubsetOf(new Set); | ||
numberSet.isSubsetOf(stringSet); | ||
numberSet.isSubsetOf(numberMap); | ||
numberSet.isSubsetOf(numberSetLike); | ||
|
||
numberSet.isSupersetOf([]); | ||
~~ | ||
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'. | ||
numberSet.isSupersetOf(new Set); | ||
numberSet.isSupersetOf(stringSet); | ||
numberSet.isSupersetOf(numberMap); | ||
numberSet.isSupersetOf(numberSetLike); | ||
|
||
numberSet.isDisjointFrom([]); | ||
~~ | ||
!!! error TS2345: Argument of type 'undefined[]' is not assignable to parameter of type 'ReadonlySetLike<unknown>'. | ||
numberSet.isDisjointFrom(new Set); | ||
numberSet.isDisjointFrom(stringSet); | ||
numberSet.isDisjointFrom(numberMap); | ||
numberSet.isDisjointFrom(numberSetLike); | ||
|
This file contains hidden or 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,102 @@ | ||
//// [tests/cases/compiler/setMethods.ts] //// | ||
|
||
//// [setMethods.ts] | ||
let numberSet = new Set([0, 1, 2]); | ||
|
||
let stringSet = new Set(["a", "b"]); | ||
|
||
let numberMap = new Map([[4, {}], [5, {}]]); | ||
|
||
let numberSetLike = { | ||
size: 1, | ||
*keys() { yield 3 }, | ||
has(x) { return x === 3 }, | ||
}; | ||
|
||
numberSet.union([]); | ||
numberSet.union(new Set); | ||
numberSet.union(stringSet); | ||
numberSet.union(numberMap); | ||
numberSet.union(numberSetLike); | ||
|
||
numberSet.intersection([]); | ||
numberSet.intersection(new Set); | ||
numberSet.intersection(stringSet); | ||
numberSet.intersection(numberMap); | ||
numberSet.intersection(numberSetLike); | ||
|
||
numberSet.difference([]); | ||
numberSet.difference(new Set); | ||
numberSet.difference(stringSet); | ||
numberSet.difference(numberMap); | ||
numberSet.difference(numberSetLike); | ||
|
||
numberSet.symmetricDifference([]); | ||
numberSet.symmetricDifference(new Set); | ||
numberSet.symmetricDifference(stringSet); | ||
numberSet.symmetricDifference(numberMap); | ||
numberSet.symmetricDifference(numberSetLike); | ||
|
||
numberSet.isSubsetOf([]); | ||
numberSet.isSubsetOf(new Set); | ||
numberSet.isSubsetOf(stringSet); | ||
numberSet.isSubsetOf(numberMap); | ||
numberSet.isSubsetOf(numberSetLike); | ||
|
||
numberSet.isSupersetOf([]); | ||
numberSet.isSupersetOf(new Set); | ||
numberSet.isSupersetOf(stringSet); | ||
numberSet.isSupersetOf(numberMap); | ||
numberSet.isSupersetOf(numberSetLike); | ||
|
||
numberSet.isDisjointFrom([]); | ||
numberSet.isDisjointFrom(new Set); | ||
numberSet.isDisjointFrom(stringSet); | ||
numberSet.isDisjointFrom(numberMap); | ||
numberSet.isDisjointFrom(numberSetLike); | ||
|
||
|
||
//// [setMethods.js] | ||
let numberSet = new Set([0, 1, 2]); | ||
let stringSet = new Set(["a", "b"]); | ||
let numberMap = new Map([[4, {}], [5, {}]]); | ||
let numberSetLike = { | ||
size: 1, | ||
*keys() { yield 3; }, | ||
has(x) { return x === 3; }, | ||
}; | ||
numberSet.union([]); | ||
numberSet.union(new Set); | ||
numberSet.union(stringSet); | ||
numberSet.union(numberMap); | ||
numberSet.union(numberSetLike); | ||
numberSet.intersection([]); | ||
numberSet.intersection(new Set); | ||
numberSet.intersection(stringSet); | ||
numberSet.intersection(numberMap); | ||
numberSet.intersection(numberSetLike); | ||
numberSet.difference([]); | ||
numberSet.difference(new Set); | ||
numberSet.difference(stringSet); | ||
numberSet.difference(numberMap); | ||
numberSet.difference(numberSetLike); | ||
numberSet.symmetricDifference([]); | ||
numberSet.symmetricDifference(new Set); | ||
numberSet.symmetricDifference(stringSet); | ||
numberSet.symmetricDifference(numberMap); | ||
numberSet.symmetricDifference(numberSetLike); | ||
numberSet.isSubsetOf([]); | ||
numberSet.isSubsetOf(new Set); | ||
numberSet.isSubsetOf(stringSet); | ||
numberSet.isSubsetOf(numberMap); | ||
numberSet.isSubsetOf(numberSetLike); | ||
numberSet.isSupersetOf([]); | ||
numberSet.isSupersetOf(new Set); | ||
numberSet.isSupersetOf(stringSet); | ||
numberSet.isSupersetOf(numberMap); | ||
numberSet.isSupersetOf(numberSetLike); | ||
numberSet.isDisjointFrom([]); | ||
numberSet.isDisjointFrom(new Set); | ||
numberSet.isDisjointFrom(stringSet); | ||
numberSet.isDisjointFrom(numberMap); | ||
numberSet.isDisjointFrom(numberSetLike); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.