-
Notifications
You must be signed in to change notification settings - Fork 17
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
Better typing for outputs #269
Comments
As per jupyterlab/jupyterlab#16764 (comment) this should be union of specialised |
With a |
Something along these lines? interface YMap<MapType> extends Iterable<[string, MapType]> {
toJSON(): Record<string, MapType>
get(key: string): MapType | undefined;
}
const dummy: YMap<number> = {} as any;
dummy.get('this key may not exists')
interface CellFormat {
source: string;
index: number;
}
type CellKeys = keyof CellFormat;
type CellValues = CellFormat[CellKeys];
interface TypedMapExplicit extends YMap<CellValues> {
get<K extends CellKeys>(key: K): CellFormat[K]
}
const clever: TypedMapExplicit = {} as any;
clever.get('source') + 'a'
2 * clever.get('source')
clever.get('this key may not exists')
// Or if we want to be generic
interface TypedMapGeneric<Interface extends Record<string, any>> extends YMap<Interface[keyof Interface]> {
get<K extends keyof Interface>(key: K): Interface[K]
}
const cleverGeneric: TypedMapGeneric<CellFormat> = {} as any;
cleverGeneric.get('source')
cleverGeneric.get('this key may not exists') |
It is a bit of work redefining the upstream methods to get them typed properly but on the upside TypeScript will catch any errors if we attempt to override them incorrectly. There are only two thing that can wrong:
|
In parallel to fixing this here, maybe we should open an upstream issue to make an interface equivalent to |
I don't think I have enough knowledge of TypeScript to do this work at the moment, and it seems you have a pretty good idea of what has to be done 😄 |
Problem
Currently outputs are not typed. This makes it hard to track changes in public API or to work with this package in general.
Previously at least the changes were typed as
nbformat.IOutput
but this changed in #241 and now they are justMap<any>
:jupyter_ydoc/javascript/src/api.ts
Lines 746 to 748 in 546a994
On the Python side the schema defines them as just an array, without a type:
jupyter_ydoc/jupyter_ydoc/ynotebook.py
Lines 35 to 46 in 546a994
The text was updated successfully, but these errors were encountered: