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

Type checked getters and setters #125

Closed
DouglasLivingstone opened this issue Mar 12, 2015 · 5 comments
Closed

Type checked getters and setters #125

DouglasLivingstone opened this issue Mar 12, 2015 · 5 comments
Labels

Comments

@DouglasLivingstone
Copy link

Following on from this post, I'm looking for a way to use cursors with TypeScript type checking. I'm not sure it fits within the scope of this project, but this is what I have in mind.

Here's a class with two properties of different types:

interface Foo {
    name: string;
    count: number;
}

var foo: Foo;
foo.name.length; // OK
foo.count.length; // Compile error

Accessing the .length property on a number is invalid, so the idea is to get an early error message at compile time, rather than only at runtime, by providing type hints.

One way of making cursors work with TypeScript is using type inference with functions to read properties instead of string keys, but doing it like this means properties are read-only. A simple workaround is to pass a writer as well as a reader in, but I'm interested in ways to avoid that without horrible hacks (for example by generating the code for the setter from the getter, and using the getter only for type information).

interface Cursor<T> {
    property<U>(getter: { (object: T): U }): Cursor<U>;
    value(): T;
}

var cursor: Cursor<Foo>;
cursor.property(f => f.name).value().length; // OK
cursor.property(f => f.count).value().length; // Compile error
@Yomguithereal
Copy link
Owner

Hello @DouglasLivingstone, thanks for the follow up. Let me document myself more about how TypeScript type checking works so I can see whether something might be improved for the library without too much pain.

@Yomguithereal
Copy link
Owner

On a slightly related matter, may I ask you if you know cursors libraries working with a type checking solution?

@DouglasLivingstone
Copy link
Author

No, I've not seen a dedicated library for it. Informally in my own code I have structures which look like this:

var field = form.field(model => model.value, (model, newValue) => model.value = newValue, ...);

which is used a bit like a cursor, but no, not really. Currently I'm considering whether to just remove the abstraction, so eg,

var field = renderField(model.value, newValue => { form.update({ value: newValue }) }, ...);

or replace it with a more general library, something that might let me write,

var field = renderField(form.cursor("value"), ...);

but not sure yet. The last example is short, but I don't see how to type-check that the form's value property matches the type of the field without ending up writing more code than it started off with.

@Yomguithereal
Copy link
Owner

Hello @DouglasLivingstone. I am afraid I haven't found a fresh solution to your problem here. Any luck on your side?

@DouglasLivingstone
Copy link
Author

No, I've not come up with anything elegant yet. If either of these tickets produce new options I'll take a look at it again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants