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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/api/observe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IObservableArray, IArrayChange, IArraySplice} from "../types/observablearray";
import {ObservableMap, IMapChange} from "../types/observablemap";
import {IObjectChange} from "../types/observableobject";
import {IObjectDidChange} from "../types/observableobject";
import {IComputedValue} from "../core/computedvalue";

import {IObservableValue, IValueDidChange} from "../types/observablevalue";
Expand All @@ -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.

export function observe(object: Object, property: string, listener: (change: IValueDidChange<any>) => void, fireImmediately?: boolean): Lambda;
export function observe(thing, propOrCb?, cbOrFire?, fireImmediately?): Lambda {
if (typeof cbOrFire === "function")
Expand Down
9 changes: 8 additions & 1 deletion src/types/observableobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ export interface IObservableObject {
"observable-object": IObservableObject;
}

// In 3.0, change to IObjectDidChange
export interface IObjectDidChange<T, K extends keyof T> {
name: K;
object: T;
type: "update" | "add";
oldValue?: T[K];
newValue: T[K];
}

export interface IObjectChange {
name: string;
object: any;
Expand Down