-
Notifications
You must be signed in to change notification settings - Fork 12.8k
API: getTypeAtLocation of node inside non-null assertion returns apparent type of TypeParameter with nullable constraint #21317
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
Yes, it is intended. We wanted to allow people to assert nonullness of type variables, since otherwise you can perform no meaningful operations on them. The nonull operator is effectively a shorthand cast, so it losing a bit of information was deemed acceptable until such time as we can represent it completely using a negated type. |
Thank you for the explanation. This actually makes sense. |
@ajafff This is likely still labeled as a bug because we haven't gotten a chance to talk about it in the office and have it retriaged yet 😉 |
Continuing discussion from #21349 here:
Let me share my use case: function fn<T extends string | undefined>(param: T) {
let foo: T;
let bar: T | undefined;
if (Boolean()) {
foo = bar = param;
}
foo!; // expected 'foo' to have type 'T', got 'string | undefined'
bar!; // expected 'bar' to have type 'T | undefined', got `string | undefined`
} Now there is no way for me to know that banging foo (
Can't you just do that |
Now that conditional types have landed, we're probably doing to change the assertion operator to narrow to |
TypeScript Version: 2.7.0-dev.20180120 (20180119 works as expected)
Search Terms:
Code
--strictNullChecks
is enabledExpected behavior:
checker.getTypeAtLocation(node)
wherenode
is the Identifierparam1
orparam2
always returns ats.TypeParameter
.Hovering
param2
in both locations shows(parameter) param2: U extends string
Hovering
param1
in both locations shows(parameter) param1: T extends string | undefined
Actual behavior:
result of
checker.getTypeAtLocation(node)
:param2
returnsts.TypeParameter
param1
returnsts.TypeParameter
param1
(passing the Identifier as argument, not the NonNullExpression) returnsts.UnionType
(string | undefined)Hovering
param2
in both locations shows(parameter) param2: U extends string
Hovering the first use of
param1
shows(parameter) param1: T extends string | undefined
Hovering the second use of
param1
shows(parameter) param1: string | undefined
The result of
param1!
is typestring
which is not valid according to #20974.This change was introduced in #20995 as a bugfix. Looking at the baseline changes this might even be intended.
IMO this is pretty inconsistent if it only applies to TypeParameters with nullable constraint.
According to the reasoning in #20974 (comment) this new behavior is invalid.
And it's a breaking change for API users.
Playground Link: Playground doesn't use the correct nightly
Related Issues:
#20995
The text was updated successfully, but these errors were encountered: