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

observe, typescript: index & mapped types #818

Closed
wants to merge 1 commit into from

Conversation

Strate
Copy link
Contributor

@Strate Strate commented Feb 3, 2017

Enable index types and mapped types on IObjectdidChange for better typecheck and inference

Enable index types and mapped types on `IObjectdidChange` for better typecheck and inference
@mweststrate mweststrate mentioned this pull request Jul 4, 2017
14 tasks
@@ -11,7 +11,7 @@ export function observe<T>(value: IObservableValue<T> | IComputedValue<T>, liste
export function observe<T>(observableArray: IObservableArray<T>, listener: (change: IArrayChange<T> | IArraySplice<T>) => void, fireImmediately?: boolean): Lambda;
export function observe<T>(observableMap: ObservableMap<T>, listener: (change: IMapChange<T>) => void, fireImmediately?: boolean): Lambda;
export function observe<T>(observableMap: ObservableMap<T>, property: string, listener: (change: IValueDidChange<T>) => void, fireImmediately?: boolean): Lambda;
export function observe(object: Object, listener: (change: IObjectChange) => void, fireImmediately?: boolean): Lambda;
export function observe<T>(object: T, listener: (change: IObjectDidChange<T, keyof T>) => void, fireImmediately?: boolean): Lambda;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting type of keys to keyof T is not quite correct in TS, unless T would represent an exact type (not supported yet).
Consider your T is { a; b; }, and your runtime observable object is { a: 1, b: 2, c: 3 } which is totally ok as it is assignable to T.
Now your listener can at most only expects "a" | "b" which is not aligned with the runtime possibility to receive "c".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sure. Same thing exists in Object.keys function definitions in lib.d.ts: this function returns string[], not a keyof passed object. I think this can stay as it is.

@Strate Strate closed this Jan 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants