-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Compile-time checking of string literal arguments based on type #394
Comments
Overload on constants already accomplishes this to some degree, although its purpose is mostly about type flow rather than errors on specific strings. The question is do you expect this to be an error: var fieldNameFromUI = askUserForInput(); // user types 'name'
get(fieldNameFromUI); // is this an error now? Are you satisfied only getting errors for string literal argument values and never when you pass string typed variables to this function? This is how overload on constant signatures work today. Alternatively they could be changed to not require a general overload that takes a string, but then it would always be an error to pass a variable instead of a string literal which we thought was not very desirable. To do better than that we'd most likely need to implement some sophisticated data flow analysis to try to understand what the literal value in a string variable is (and it would still fail to figure out the value with some frequency). |
Definitely not looking for any sort of run-time checking. As far as data flow analysis, ex:
I can see that being tricky and personally would not mind if that did not throw any error. I'm not sure if I personally have an opinion on whether or not variables would be allowed at all. I think my leaning would be "yes, variables are allowed". The main issue that I'm trying to solve is that in certain cases you absolutely have to specify a string to access a property and it would be nice if in those cases you could get some compile-time checking so that refactoring/etc is less scary. |
So in that case if we altered the requirements for overload on constant signatures you could get some of this without changing much. Today: function get(property: 'firstName');
function get(property: 'lastName');
function get(property: 'phoneNumber');
function get(property: string); // this is a required signature today
function get(property: string) {
// do the getting
}
var result = get('firstName'); // ok
var result2 = get('somethingElse'); // ok
var x = 'hi';
var result3 = get(x); // ok Alternate reality: function get(property: 'firstName');
function get(property: 'lastName');
function get(property: 'phoneNumber');
//function get(property: string); // this is no longer required
function get(property: string) {
// do the getting
}
var result = get('firstName'); // ok
var result2 = get('somethingElse'); // error
var x = 'hi';
var result3 = get(x); // ok It's a little more verbose to declare than your 'memberof' proposal but it does give you that error checking. It's not clear how often you'd be able to define an API like this that only ever used string literals and never string typed variables though. |
The problem with the overload on constant signatures is that it requires you to separately define your string constants apart from the actual interface you're working on. A major part of my proposal is that you can use an existing interface to define the valid property names. In other words, let's say I was writing the TypeScript definition file for Backbone. The definition file today looks something like this:
It would be much better if I could write this:
Another example of an API where you would (mostly) use a string literal instead of string typed variables is React: http://facebook.github.io/react/docs/two-way-binding-helpers.html In particular, the "linkState" function takes an argument which is the name of a property on the state object. Since my state object has an interface it would be ideal if I could ensure at compile-time that the string literal I typed is correct. |
I would reference old proposals about extending enums: |
Yeah, I definitely get how overload on constants doesn't scale super well here. Are there many other frameworks where this would be valuable? There's a high bar for adding new syntax so we'd want to ensure it's solving a reasonably large class of issues. We also need to better understand whether it'd be generally useful to be able to define overloads that can only take string literal values and not computed values/variables. |
Regarding other frameworks, I believe this is a very common pattern. I've spent 30 minutes putting together a list. Based on a "model"
Utility mechanisms that could still potentially benefit |
Great examples, very useful to guide our thoughts, thanks. Looking at your earlier examples again, how do you feel about this returning class Model<T> {
...
get(property: memberof T): any;
...
} You'll get nice checking on the input side now but you're going to have to cast on each getter call or else deal with a lot of unsafe code. To do better than that will require some other non-trivial new features which @RyanCavanaugh and I were just talking about and need to be fleshed out a little further. |
I thought about the return type but left it out as to not make the overall suggestion too big of a bite. This was my initial thought but has problems. What if there are multiple arguments of type
This solves the "which argument am I referring to" problem, but the
I think this works better.
Unfortunately not sure that I have a great solution although I believe the last suggestion has decent potential. |
This seems some kind of 'type maps'. Defining this manually will allow potentially more functional string-type enums: (from my post) // Has same syntax with interfaces but works only as a type map
typemap Foo {
madoka: MadokaObject;
homura: HomuraObject;
/* ... */
}
class GameCenter {
// Much shorter function declaration
createCharacter(charType: Foo is A): A {
}
} |
Another related proposal would be to add the At compile time, Using
|
I am currently using Baobab for a project, it is an immutable data tree / cursor library. When using it I have to use something like this (non-working simplified example): type Foo = {
a: number;
};
get<T>(property: string): T;
// elsewhere
get<number>('a'); This for one does not check that the property actually exist and also forces me to cast the This suggestion would therefore be really useful to help keep my code as type checked as possible. I like this proposal from @jbrantly:
My proposal would be something like this:
The righthand would need to be an object type ({...}, interface, Class) or it would throw an error. If you try to pass something that doesn't resolve to a constant string to a memberof type it would throw an error. Or a way to reference a subtype using the parameter name:
|
the proposal in #1295 should cover the scenarios outlined in this issue. |
Looks like this is largely solved by string literal types: https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#string-literal-types ? |
Not really, but I think at least it is a prerequisite to solve this issue. #10425 will do. |
Often a method will take as a string the name of a property of some object that it's working on. A common example would be http://backbonejs.org/#Model-get
It would be ideal if TypeScript could provide compile-time checking on these calls to make sure that the string is actually a valid property of the underlying model. As a possible proposal:
In the context of the get function, "memberof Person" would be equivalent to "string".
The text was updated successfully, but these errors were encountered: