This project is read-only.

TypeCheck on properties and properties type

Topics: General, Language Specification
Jan 29, 2014 at 2:07 AM
Edited Jan 29, 2014 at 2:09 AM
When trying to use some JavaScript patterns that I'm familiar with, I often ends up being confronted to the following problem :
class Component<T> {
    state: T;
    setState(t: T) {
        this.state = t;
    render() {

interface Property<T> {
    getValue(): T;

class ReactiveComponent<T> extends Component<T> {
    setValueProps(values: { [prop: string]: Property<any> }) {
        this.setState(<T>Object.keys(values).reduce((state, property) => {
            state[property] = values[property].getValue();
            return state; 
        }, {}))
I would like to have the possibility to say that the values parameter of the setValuesProps should have a relation with state, without any possibility to do so.

In the same way I would like to be able to say that a method take a 'property' of an interface/class as parameter and typeof this property as return value, like for example with backbone-like model :
interface Contact {
    name: string;
    age: number;

interface Model<T> {
    set(name: string, value: any): void
    // I would like to be able to tell to the compiler that name should be a 
    // property of contact  and that value should be typed to the type of this property

var contactModel: ModelContat
contactModel.set('name', 40) // give an error
Perhaps a notation like :
interface Model<T> {
    set<U propertyof T, V typeof U >(name: U, value: V): void
Could resolve some of this problems, but not the first one .

Does someone have workaround/ideas to describe this kind of pattern?
Does the TypeScript team has plan/idea of future feature that could solve the cases presented here ?