-
-
Notifications
You must be signed in to change notification settings - Fork 570
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
Improve types for Y.Map's public interface #614
Open
Smona
wants to merge
10
commits into
yjs:main
Choose a base branch
from
Smona:improve-types-for-map-public-interface
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e9a8af8
Improve types for get(), set(), and MapType
Smona ea3f171
Fix types of entries argument & _prelimContent
Smona 6802f8d
Fix entries validation
Smona cd79765
Merge remote-tracking branch 'origin/main' into improve-types-for-map…
Smona 4f49c58
fix type errors in y-map.tests
Smona b97920b
add Doc as possible YMap value
Smona c9995bd
constrain getMap type
Smona 8c1d7c7
fix type errors in tests
Smona 5d0da80
type output of map.toJSON
Smona f22dbb7
add test file for types
Smona File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,92 @@ | ||
/** | ||
* This file serves to validate the public TypeScript API for YJS. | ||
* | ||
* It is not included in `npm run lint` or any other automated type checking, but can be used | ||
* by those working on YJS types to ensure that the public-facing type interface remains valid. | ||
* | ||
* Any lines which are supposed to demonstrate statements that _would_ generate type errors | ||
* should be clearly marked with the type error that is expected to result, to provide a | ||
* negative test case. | ||
*/ | ||
|
||
import * as Y from "../dist/src/index"; | ||
|
||
/* | ||
* Typed maps | ||
* | ||
* - Key names are autocompleted in first parameter of `get` and `set`. | ||
* - `MapType` value types are constrained to valid Y.Map contents. | ||
*/ | ||
type MyType = { | ||
foo: string; | ||
bar: number | null; | ||
baz?: boolean; | ||
}; | ||
|
||
// Constructor argument keys & values are typechecked, and keys are autocompleted. | ||
// Multiple items for each key and partial initialization are allowed. | ||
const map = new Y.Map<MyType>([ | ||
["foo", ""], | ||
["foo", "even better"], | ||
// ERROR: Type '["baz", number]' is not assignable to type '["foo", string] | ["bar", number | null] | ["baz", boolean | undefined]'. | ||
["baz", 3], | ||
]); | ||
|
||
// Entries are still allowed to be omitted, so get() still returns <type> | undefined. | ||
const defaultMap = new Y.Map<MyType>(); | ||
|
||
// `json` has a type of `Partial<MyType>` | ||
const json = defaultMap.toJSON(); | ||
|
||
// string | undefined | ||
const fooValue = map.get("foo"); | ||
// literal "hi" (string) | ||
const fooSet = map.set("foo", "hi"); | ||
// number | null | undefined | ||
const barValue = map.get("bar"); | ||
// ERROR: Argument of type '"hi"' is not assignable to parameter of type 'number | null'. | ||
const barSet = map.set("bar", "hi"); | ||
// ERROR: Argument of type '"bomb"' is not assignable to parameter of type 'keyof MyType'. | ||
const missingGet = map.get("bomb"); | ||
// Escape hatch: get<any>() | ||
const migrateGet = map.get<any>("extraneousKey"); | ||
|
||
// ERROR: Type '<type>' does not satisfy the constraint 'Record<string, MapValue>'. | ||
const invalidMap = new Y.Map<{ invalid: () => void }>(); | ||
const invalidMap2 = new Y.Map<{ invalid: Blob }>(); | ||
// Arbitrarily complex valid types are still allowed | ||
type ComplexType = { | ||
n: null; | ||
b: boolean; | ||
s: string; | ||
i: number; | ||
u: Uint8Array; | ||
a: null | boolean | string | number | Uint8Array[]; | ||
}; | ||
const complexValidType = new Y.Map< | ||
ComplexType & { nested: ComplexType & { deeper: ComplexType[] } } | ||
>(); | ||
|
||
/* | ||
* Default behavior | ||
* | ||
* Provides basic typechecking over the range of possible map values. | ||
*/ | ||
const untyped = new Y.Map(); | ||
|
||
// MapValue | undefined | ||
const boop = untyped.get("default"); | ||
// Still validates value types: ERROR: Argument of type '() => string' is not assignable to parameter of type 'MapValue'. | ||
const moop = untyped.set("anything", () => "whoops"); | ||
|
||
/* | ||
* `any` maps (bypass typechecking) | ||
*/ | ||
const anyMap = new Y.Map<any>(); | ||
|
||
// any | ||
const fooValueAny = anyMap.get("foo"); | ||
// literal "hi" (string) | ||
const fooSetAny = anyMap.set("foo", "hi"); | ||
// Allowed because `any` unlocks cowboy mode | ||
const barSetAny = anyMap.set("bar", () => "hi"); |
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
Oops, something went wrong.
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.
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.
note this logic change, but if
_prelimContent
was null previously then this would have created a runtime error.