-
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
Bug: No null checks in type parameters #17478
Comments
It is different indeed, but it is assignable too. Why not just delete the function map(list: List<number>): List<number> { return list; } |
In fact, I discovered this when investigating a type bug in a bigger application where the TypeScript compiler should have complained about a type mismatch but didn't. The above code is not the actual use case, but just the minimal example that produces the same error. Yes, I could delete the Why do you think that both types are assignable? |
|
Yes, The above example illustrates this very well: we can suddenly add They recognized this problem in Java as well, and this is exactly the reason why you can assign |
The problem is that TypeScript doesn't support variance annotations (#1394). If the return list's parameter had been marked as covariant |
If you are explicit about types... While you don't consider it a cast, you are relying upon asserted types and TypeScript is checking that the types are assignable, which they are. I don't see how it become un-type safe if you explicitly widen a type. |
@mckauf Java has use-site variance declarations. public static void main(String[] argv) {
List<String> aList = new ArrayList<>();
List<? extends String> covariantList = aList;
List<? super String> contravariantList = aList;
String o2 = covariantList.get(0); // we can read `String`/`Object`
Object o = contravariantList.get(0); // we can read only `Object`
covariantList.add(null); // we can add only `null`
contravariantList.add("Test"); // we can add String and its derivatives (aham..)
map(Arrays.asList(1.0)).add(null); // practically read-only, we can add only `null`
}
public static List<? extends Number>map(List<Double> l) {
return l;
} |
Because casting I have pointed out why. You can also read this to understand the problem: http://onewebsql.com/blog/generics-extends-super In Java there are boundaries for type parameters ( |
See #1394. |
TypeScript Version: 2.2.2
Code
Expected behavior:
Compiler should complain about line 6 (
return list
) whenstrictNullChecks
is enabled, becauseadd(elem: number): void
has a different signature thanadd(elem: (number | null)): void
.Actual behavior:
This above code goes through the compiler, even though
strictNullChecks
,noImplicitAny
et c. are all enabled. This results in that we can addnull
to aList<Number>
in functionfoo
and the compiler does not complain.The text was updated successfully, but these errors were encountered: