-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Error 2775 ("explicit type annotation required") for class method type assertions, when instance type seems obvious #47945
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
Even obvious inferences require inference to happen before the compiler can know about them. Inference is done in a single pass and requires knowledge of where all the assert calls are before it runs. |
It might seem like declare const AssertingClass: {
new(): SomeType;
new(arg: number): SomeOtherType;
} |
Ah, interesting point @RyanCavanaugh, I had not looked at it like that. Thanks for the reply! Do you think there might be a path to supporting our use case (exporting a class which uses asserts, without requiring end-users to add type annotations) in future? |
It's always possible we'll get smarter in the future, but I don't see a straightforward way to fixing that particular scenario right now without incurring the sort of performance penalty (10%+ on all programs IIRC) that has kept us from doing this analysis so far. |
That's good to know. Thanks for the help and for all the great work you and your team are doing! |
Perhaps this limitation could be re-investigated after migrating the compiler to Go? |
Bug Report
We have been experimenting with the
asserts
keyword for type assertions, which seems like a really powerful feature, but we are hitting the issue that class methods which useasserts
require an explicit type annotation on the instance (Assertions require every name in the call target to be declared with an explicit type annotation.(2775)
).It seems "obvious" to me in the code sample below that the type of
assertingInstance
isAssertingInstance
, so I don't understand why it requires an explicit type annotation. For our use case (authoring a library), the requirement for users to add an explicit type annotation when consuming the library makes usingasserts
a non-starter unfortunately.I have read all the existing issues I can find for this issue, and I do understand why this is required for function calls as described in 45385 and 34596, but I'm not clear on why this should apply for the case of an instance of a class. Perhaps I'm missing some way in which the type is not as "obvious" as it seems?
It would be really nice if we could use this feature on classes without explicit annotations, but if there are technical reasons why this isn't possible, I'd love to understand them and to know if this might one day be possible. I'd also be interested to know if there are any workarounds which do not require consumers to add an explicit type annotation.
🔎 Search Terms
type assertion, asserts, explicit type annotation, 2775, class, instance, method
🕗 Version & Regression Information
v4.5.4
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Error 2775: Assertions require every name in the call target to be declared with an explicit type annotation.(2775)
🙂 Expected behavior
Typescript to infer that the type of
assertingInstance
isAssertingClass
without the type annotation, and so allow theasserts
keyword ot be used without an explicit annotation.The text was updated successfully, but these errors were encountered: