-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type assertion error should say something about assertion rule #10360
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
Comments
As I understand it, type assertions require one of the types to be assignable to the other, either way. In your example, neither the To make that clearer, here are the equivalent types and related assignments: interface Foo {
bug(): PromiseLike<number>;
bar(): void;
}
interface Baz { // this is the type of `foo` in the OP example
bug(): Promise<number>;
}
var foo: Foo;
var baz: Baz;
foo = baz; // ERROR: 'Baz' is not assignable to 'Foo'. Property 'bar' missing in type 'Baz'...
baz = foo; // ERROR: 'Foo' is not assignable to 'Baz'... 'PromiseLike<>' is not assignable to 'Promise<>'... |
@yortus Ah thanks, that is understandable. But then I think the error message here is confusing as it does not say anything about the type assertion rule. |
I don't think every error message should recite the relevant spec section, but we do see this reported fairly often. Would something like this be useful?
|
That is helpful :D |
While "assert" reflects the name of the syntactic form ("type assertions"), I wonder if "treat ... as" is more understandable.
|
For type assertion error, I usually found that the fix suggestions ("Property 'p' is missing in 'T'") misleading. By definition, if types T and U are not either-way-assignable, there must be The actual fix for type assertion error may be done on T or U, or both, by adding or removing propertys, or changing the assertion itself, but the compiler only suggests one approach. I suggest not providing fix suggestions, instead just pointing out the properties in trouble.
So the error message of the first test case in this thread could be:
I also suggest that the compiler assigning a random type name(e.g. |
I believe this has been addressed by #25541. |
I can confirm that the message has been updated:
I'll close this issue, feel free to open a new one (and link back) to give more suggestions. 👍 |
TypeScript Version: master branch
Code
Expected behavior:
Type assertion should work here.The error message should say about the assertion rule.The text was updated successfully, but these errors were encountered: