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 meta dependencies #751

Closed
NoelAbrahams opened this issue Sep 25, 2014 · 3 comments
Closed

Type meta dependencies #751

NoelAbrahams opened this issue Sep 25, 2014 · 3 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@NoelAbrahams
Copy link

Consider this scenario.

  • An external web service that stores arbitrary JSON objects and provides querying services.
  • A sample object could look like { "id": 100, "name": "Joe Bloggs" , "address":"1 London"}
  • As users of this web service, we add and retrieve these "Person" objects and use it widely around the code base.
  • Consequently, it makes sense to type this object:
interface Person {
   id: number;
   name: string;
   address: string;
}
  • This is just great, because now we have compile time safety around most of the interaction with the external web service.
  • There is, however, a small hole: the web service specifies the object that should be passed when querying the service as follows:
    interface Query {

          term: string; // The search term
          select: string; // A comma separated list of columns to return
    }

And we define the object as follows:

        var query: Query = {
            term: 'Joe',
            select: 'id,name'  // This is the hole
        };
  • The property names defined here are obviously outside of the type system and are a potential source for bugs.
  • In order to bring this into the type system we carry out the following hack:
       // Need to do this everywhere Query is used to request a Person
        var typeCheck: Person = <any>{};
        typeCheck.name;
        typeCheck.id;

        var query: Query = {
            term: 'Joe',
            select: 'id,name'
        };

The idea being that if someone changes, say, Person.name to Person.firstName then that would highlight the hidden reference to name in the string literal.

A similar problem was highlighted in #394.

I wonder if TypeScript could formalise this somehow:

        var query: Query <depends on meta of Person> = {
            term: 'Joe',
            select: 'id,name'
        };

so that it's not possible to change Person without breaking this code.

@danquirk
Copy link
Member

danquirk commented Oct 7, 2014

As you noted #394 is similar but also seems simpler and more obviously applicable to a wide variety of scenarios. This seems like a somewhat awkward way to enforce a fairly specific pattern, unless you're saying this kind of thing is super common in a lot of frameworks. This is getting into some complex dependent type mappings or something.

@danquirk danquirk added Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds Out of Scope This idea sits outside of the TypeScript language design constraints labels Oct 7, 2014
@danquirk danquirk closed this as completed Oct 7, 2014
@NoelAbrahams
Copy link
Author

Yes, it's specific. The intention was to provide a use-case that will hopefully be taken into account when resolving #394

@danquirk
Copy link
Member

danquirk commented Oct 8, 2014

Got it. There is definitely a large class of problems that our type system can't currently model with all these string based JavaScript APIs the tie string values to property names, events, etc. I'm hoping there are some targeted fixes we can make in the compiler (like #394) and tooling (like #606) that will improve things a bit but there will definitely still be holes. I'd love to come up with a more general, holistic approach but a lot of these APIs do things just differently enough to make it hard to see how to model them all well.

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds
Projects
None yet
Development

No branches or pull requests

2 participants