-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Consider resolving of Control flow analysis's confusing with type mismatches in initialization #9886
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
If assignment at the point of declaration was an error, but the same assignment on the line after the declaration was ok, wouldn't that also be a confusing/unclear behaviour? |
It means const o1: A<void> | B<void> = new B<void>(); // error
let o3: A<void> | B<void>;
...
o3 = new B<void>(); // ok Another, function f(o: A<void> | B<void> = new B<void>()) { // ok
o = new A<void>(); // ok
} |
This currently compiles fine. Are you suggesting it should be an error? If so, how else could you declare a default value for a parameter that had a union type? |
It is a correct case. Updated that example. |
So the last line in this example would be an error under this suggestion? type Optional<T> = Some<T> | None;
interface None { readonly none: string; }
interface Some<T> { readonly some: T; }
const none : None = { none: '' };
class Foo {/***/}
let maybeFoo: Optional<Foo> = none; // becomes an error? |
Yes, it needs an assertions. You shouldn't modify variables under this suggestion, and should use let maybeFoo: Optional<Foo> = none; // error
let maybeFoo: Optional<Foo> = <Optional<Foo>>none; // ok |
This doesn't appear consistent with your original suggestion, which shows an example of a Bear in mind that I'd say it's unclear at best what problem this is solving and what kind of code you are aiming to encourage here. Perhaps you could revise the suggestion to show the kind of use of |
I really can't come up with a practical case for where this is desirable. I don't know of any situations where I locally want to tell the type system that I don't want it to know the actual type at a given location. The best thing I can imagine is when you're just trying to experiment with code to see what the apparent members are of a complex type, but in those cases, you can just use the |
Adding
--strictInitialization
new option is helpful to clear behaviors of Control flow analysis.Problem
CFA is not clear when declaration type and initialization type are not a same type.
related: #9859
Solution
Disallow initialization by different type.
The text was updated successfully, but these errors were encountered: