Skip to content
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

TypeScript enhancement #490

Open
himself65 opened this issue Jan 2, 2023 · 2 comments · May be fixed by #614
Open

TypeScript enhancement #490

himself65 opened this issue Jan 2, 2023 · 2 comments · May be fixed by #614
Assignees

Comments

@himself65
Copy link
Contributor

himself65 commented Jan 2, 2023

Currently, yjs do have some typescript decorations for basic usage, but it needs to be stronger and more understood by people in typescript.

For example,

export class YMap<MapType> extends AbstractType<YMapEvent<MapType>> implements Iterable<MapType> {
// ...
}

In this type, MapType is the value allowed by this map. But there are no key restrictions for this type. So in the downstream code, I have to write many as for typecasting.

Also, there are no restrictions on both in yDoc.

const yMap = doc.getMap('x') // not a good type
const yMap2 = doc.getMap('x') as Y.Map<string | number>
yMap.get('1') // this is string or number

The expected types

This is an example from my repository that enhances the map type.

import type { Map as YMap } from 'yjs'

export interface ExtendsYMap<Data extends Record<string, unknown>, Keys extends keyof Data & string = keyof Data & string> extends YMap<any> {
  clone (): ExtendsYMap<Data, Keys>

  delete (key: Keys & string): void

  set<Key extends Keys> (key: Key, value: Data[Key]): Data[Key]

  get<Key extends Keys> (key: Key): Data[Key]

  has<Key extends Keys> (key: Key): boolean

  clear (): void
}
@himself65
Copy link
Contributor Author

@strangecamelcaselogin
Copy link

While this is a more general issue, I think it is related to #352

@Smona Smona linked a pull request Sep 27, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants