Skip to content

Instantly share code, notes, and snippets.

@aiya000
Last active July 8, 2019 05:15
Show Gist options
  • Save aiya000/5f12ca0276eabaf6bf1331ee2cd96fae to your computer and use it in GitHub Desktop.
Save aiya000/5f12ca0276eabaf6bf1331ee2cd96fae to your computer and use it in GitHub Desktop.
Typgin NativeScript's untyped Observable
import * as Untyped from 'tns-core-modules/data/observable'
import deprecated from 'deprecated-decorator' // npm install --save-dev deprecated-decorator
/**
* ValueTypes<{foo: number, bar: string}> = number | string
*/
type ValueTypes<T> = T extends { [k: string]: infer I }
? I
: never
/**
* Field<'foo', {foo: number, bar: string}> = 'foo'
*/
type Field<K extends string, T> = K extends keyof T
? K
: never
/**
* Don't dirty your hands.
* You must use this instead of [[Untyped.Observable]].
*
* Unfortunately, still this is a little type unsafe.
* Please see the comment of [https://github.com/aiya000/workspace/blob/7cc4c5e53a74036e9c2339cc39d6ee0f47f8a315/TypeScript/ConditionalTypes/type-untyped-method.ts].
*/
export default class Observable<T extends object> extends Untyped.Observable {
constructor() {
super()
}
/**
* A typed safety set()
*/
public assign<K extends string>(name: Field<K, T>, value: ValueTypes<T>): void {
super.set(name, value)
}
@deprecated('assign')
public set(name: string, value: any): void {
super.set(name, value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment