google / google/closure-compiler
JSType.toAnnotationString loses type information
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
When using the `toAnnotationString()` method on a `JSType` instance then the returned string is not the same as the originally parsed type annotation and for object types the non-null information is lost.
To visualize the problem here is a table from orginal annotation to the output of `toAnnotationString()` and the output of this method again when a file which is generated from this output is parsed again:
```
string -> string -> string Correct
!string -> string -> string Different, but correct
?string -> (string|null) -> (string|null) Different, but correct
Object -> (Object|null) -> (Object|null) Different and redundant, but correct
!Object -> Object -> (Object|null) WRONG!
?Object -> (Object|null) -> (Object|null) Different and redundant, but correct
```
So as you can see the processing of the three `string` types is basically correct. But the processing of the `Object` types is wrong. The redundant null-information is technically not a real problem, just bloat, but the real problem here is the lost non-null information. So when using `toAnnotationString()` to generate source code (Or externs) then the generated source code is wrong.
Contributor guide
Assessment
This issue has not been assessed yet.