-
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
Weak type checking for classes with unreferenced generic parameters #7797
Comments
The typing is structural, not nominal. The typechecker doesn't have a Why didn't you declare |
That is exactly the programmer mistake the compiler did not catch here. Don't blame the programmer! They're only humans! |
Ah, right. Does it still give the green light with |
Maybe in the way it is formulated now, but that isn't really the point.. The programmer could have written: class Bag<T> {
items: any[] = [];
} Would you recommend "blaming" them again for doing something that's not really optimal and "punish" them by hiding from them the simple fact they assigned |
With |
Now you blame the programmer again by telling them they should go and study how "structural typing" works! In any case that is not a necessary property of structural typing! why would you think that way? It has to do with how [class] generics were applied on top of structural typing in this particular language. |
The only thing I can get out of this is that "generic type not used in type definition" is probably a useful rule for tslint. |
If I write a function: function sum(xs:number[]) {
return 4;
} Should this be an error? A typechecker or static analysis tool can't tell what What you've done is the same thing, except with types rather than values. You've defined a generic type and not referenced it in the definition. The compiler can't tell that what you wrote wasn't what you intended. It can only work with the information it has. The only useful thing it can do is warn that you have a type parameter which you didn't use, which was probably unintentional. That's not a hard rule though, so it belongs in TSLint. That doesn't help you if you used |
The scope of the problem I'm describing here, as of now, does not include interfaces (at least at this point). I'm only talking about identically defined and implemented classes or ones that are related by explicit inheritance. For interfaces it's a different story and I currently see that as less important. When you have two entities that were defined and implemented exactly the same way with exactly the same code and logic, being assigned with incompatible signatures to one another is practically always a mistake. This is a very different story from interfaces. For interfaces it may be that a linter approach may be sufficient, maybe not. I haven't looked at them that much at this point. |
You've already had a response from the TS team regarding this exact topic in #7792 (comment) and #7792 (comment). I'm not sure what else you're expecting by opening another issue. |
I don't see this as a trial, and they are not judges, and nobody should feel like they are being accused or need to become a lawyer to protect anyone "else". This should be a mostly pragmatic discussion on the type checker allowing The only thing is that incidentally it turns out is that fixing this touches on more fundamental issues of how generics were applied to classes and may "embarrass" the original designers of the language, people like @ahejlsberg etc., whom I personally invite to comment here. I believe the team members are simply trying to avoid the uncomfortable situation where they will have to re-think and re-discuss this, and prefer simply forgetting it and moving on. |
Unless you can cite an actual problem besides "it's not what I expected" then there is no "pragmatic discussion" to be had and this issue can be closed. |
I don't understand what you want exactly and why do you care if someone reported an issue about something they felt was important enough to open a discussion for. The fact this is an issue that dates back to the original design doesn't mean it cannot be addressed. It starts to feel like the only people who are in the position to consider this patiently and rationally are the original designers of the language. Participators and team members seem to tend to go with the "lawyer"/"diplomat" approach for the more fundamental aspects which are mostly out of their impact, which I feel is inappropriate. I'm sorry but this type of negative attitude really discourages people from participating here. |
By a "pragmatic" attitude I meant looking at this design problem with an open mind and the goal of preventing programmer mistakes rather than strongly adhering to a rigid idea and justification of "what class generics in structural typing means and how it must be". The only reason I closed the issue was because the atmosphere has become unreasonably hostile and negative. This surprises me for a forum that is supposed to attract people who are interested in language design, who I would expect would approach new suggestions openly and constructively. |
TypeScript Version:
1.8.9
Code
Expected behavior:
Since
Bag<number>
is not assignable toBag<string>
,putStringsInTheBag(bagOfNumbers)
should error.Actual behavior:
No error
The text was updated successfully, but these errors were encountered: