-
Notifications
You must be signed in to change notification settings - Fork 164
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
add a type to represent the undefined value #60
Comments
Ideally like nullability we have undefinability. This could even replace the optional argument syntax. Using (Looking at JavaScript proper perhaps nullability ought to be the special case and undefinability ought to use |
We should also reconcile this with void. Perhaps get rid of the void and just have undefined. Seems better and clearer. |
This comes up now and then with defining map-like constructs, where JavaScript returns undefined, but we can only do something nullable or go straight to any (e.g., @jakearchibald encountered this with service workers, where something had to be a |
See also https://www.w3.org/Bugs/Public/show_bug.cgi?id=27549 by the way, which suggests using "optional" as syntax. |
In our case it was a promise that resolves with caches.match(url).then(response => …); If no match is found, it resolves undefined. We've had to go with "any" to work around it, which isn't very readable. |
I've got another case of this, and it's also about maplikes - I want to write a custom (I first tried to use Perhaps just repurpose the existing |
"Void" might make sense for attributes and return values, but for method arguments we use "optional". For dictionary members it's "not present". I guess if we add some explanation around it's a good first step, but if we want to harmonize more with JavaScript syntax explicit undefined or dedicated syntax might be better? |
Could we take this over the finish line by settling the lingering syntax questions? I'm not a fan of this being addressed with special syntax, like I don't have a strong opinion either way, it would just be useful to have this issue settled and it should be a pretty easy fix. |
|
Why would |
I mean weirder than |
All right then, I'll write a PR going for |
I would prefer aligning on undefined as that's how JavaScript calls it and it would be annoying to have to talk about the void value in algorithm prose. It would also be more familiar for those new to IDL. Although maybe someone knows the reason TypeScript has both void and undefined https://www.typescriptlang.org/docs/handbook/basic-types.html#void? If there's a good reason we might want to follow that. |
|
It seems that would argue for IDL just having undefined if we want to take inspiration from TypeScript and JavaScript? Edit: note that another change we could make (if it doesn't make the parser ambiguous, not sure) is to allow/require omitting the return type of methods if it is undefined. |
(Another reason is just that it looks unfamiliar, though.) |
Do you think we could just write |
That might be a solution if it adds no ambiguity. |
That would probably make the grammar no longer LL(1), since the parser would have to look ahead two tokens to find the |
Sorry to bring this issue up, but I already miss the On the other hand, |
For a hypothetical |
Well in my understanding, for functions it adds a particular meaning ---> no return value, which is different to return |
function test(): void {
return undefined;
} is fine per https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABFApgZygCgJQC5EBucMAJogN4BQiiATilCLUuCSsDGCiQNyUC+QA. And it would be somewhat weird if it was not as a function lacking a return statement and one returning undefined are identical in JavaScript. If you use IDL for other languages you can continue to assume that a function whose return type is exclusively undefined returns nothing, just like it returns "nothing" in JavaScript. |
There are theoretical points for However, a good explainer is due (a digest of this issue discussion might serve as one). I would aesthetically prefer to say Between returning Applying Occam's tool indeed suggests getting rid of |
This is not fine however: function test(): undefined {
console.log("hey")
} |
It's possible that the needs of TypeScript and Web IDL diverge, and that's OK. Web IDL intends to model JavaScript, where functions that don't return anything, return There is no distinction in JavaScript between functions that "actually don't return anything" vs. "returning |
|
TypeScript uses |
….get() whatwg/webidl#60 has been fixed for a long time.
It's too awkward to deal with the undefined value by having to use
any
. Let's introduce a new type (undefined
?) to represent that one value. Some care will be needed to make sure we handle optional arguments, distinguishability, etc. properly.The text was updated successfully, but these errors were encountered: