-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Non-null assertion operator and the typeof operator #17370
Comments
I think you're asking for #6606. For now, workarounds involve forcing the type checker to evaluate the type you care about while not causing much extra work at runtime: class Foo {}
class Bar<T extends Foo> {}
declare const x : Foo | undefined
if (false as true && (typeof x !== 'undefined')) {
var definedX = x; // assignment never happens at runtime
}
const y = new Bar<typeof definedX>() |
i mean, yeah #6606 would definitely solve this as it's a pretty all encompassing proposition... It would be nice if there was a better error message to signify that the error is specifically that the expression isn't supported, rather than a 'semicolon expected' error. |
A potential alternative here would be for the compiler to expose |
I'd be interested to get their take on it. Evidently they didn't feel the expression level had too many operators to add that Edit: to get to your point, yeah, if we are to be stuck with a type level not on par with the expression level, that admittedly goes to validate the added value of your expression-first #6606 variant. |
We can expand the grammar here bit by bit without going full-blast with #6606. This incremental change seems pretty reasonable |
it would be great as (right now) there's no other way to scrub |
@RyanCavanaugh Hm, I'd prefer full-blast (you might have already got this 😄) as it would cover much more cases at once. |
@bradzacher: I think you meant type level. :) |
@RyanCavanaugh: on #6606, does the reluctance stem from perceived complexity of the implementation as you proposed it (link)? Because afaik, the proposal would not necessarily* bring breaking changes. *: ignoring the keyword priority discussion raised by Igorbek for the expression-based proposal. |
#6606 is accepting PRs and anyone can jump in, but we're not ready to commit team resources to it yet. The prioritization is based on a cost/benefit analysis: adding |
@RyanCavanaugh is #6606 accepting PRs even in expression-based syntax? |
On-topic, with 6606 a type-level class Foo {}
// expression level function application today, to test:
declare function assert<T>(v: T | null | undefined): T;
let a: Foo | undefined;
let b = assert(a); // Foo
// v type level function 'application' with #6606:
type Assert<T> = (<U>(v: U | null | undefined) => U)(T);
let x: Assert<Foo | undefined>; // Foo |
from the looks of it - typescript doesn't let you have self invoking functions like that within a
|
@bradzacher: yeah, the function 'application' there is not possible yet. Either flavor of the 6606 proposal could enable something similar to that. |
since we're just about down to aesthetics... welcome to |
I made some initial progress at #17948, but still broken. |
Seems they reconsidered in favor of your conclusion:
|
Working example at 4d14c9f. |
This should be doable today with |
@mhegazy it should be yeah! 👍 I would have liked the operator for it for consistency with non-type def syntax, but I understand and agree with the design decisions. I see it was merged a few weeks ago. I'd assume that'll be part of the next minor version (2.8??). |
|
Closing as fixed by |
TypeScript Version: 2.4.1
Code
Expected behavior:
the
t1
statement should create atype
of typestring
.the
t2
statement should create atype
of typestring
.Actual behavior:
the
t1
statement errors with';' semicolon expected
.the
t2
statement creates atype
of typestring
.There appears to be no way to achieve this without defining a separate variable, or by type guarding to remove the
undefined
/null
.With strict null checks on, this is especially troublesome in the case of generics with extends clauses, i.e.
It would be great if the non-null assertion operator worked natively in typeof statements, or even if you could use brackets to resolve the assertion before the typeof.
The text was updated successfully, but these errors were encountered: