-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Improved typechecking error for unstable props #5503
Improved typechecking error for unstable props #5503
Conversation
Not really. I mean there maybe ways to hack the compiler itself. One thought. I was thinking of having a mirror of the unstable APIs to use in unstable mode, where each would simply be of type Another thing is we could set up a type alias of |
@kitsonk @lucacasonato we should prioritize this issue; there's a lot of questions related to this problem. I like the @kitsonk's idea, but if it requires complicated changes then Luca's solution is also fine. |
I'll prototype @kitsonk's idea. |
I just tried out some things: // lib.deno.ns.d.ts
export const hostname: never;
// test.ts
const x = Deno.hostname(); which leads to a pretty unhelpful error message:
// lib.deno.ns.d.ts
export function hostname(): never;
// test.ts
const x = Deno.hostname(); which leads to no TypeScript error, instead errors at runtime:
This PR: // lib.deno.ns.d.ts
// test.ts
const x = Deno.hostname(); which leads to no TypeScript error:
I don't think the never thing will work unless I am doing something wrong. I think |
I got tripped up at least a few times trying to use an unstable function while learning Deno... Agreeing with kitsonk, we should try to minimize the collateral damage. We're talking about handling error reporting for unstable functions here... if the solution is a hacky one I don't think it'll really matter in the long run once the functions are stable! But I've been thinking about this for a while and I'd like to add an alternative (which may be more or less hacky, I'm newish to typescript so sorry if my code smells a bit lol) I can see here where the flag causes unstable stuff to be added. What if we make the unstable properties throw during runtime? Something along these lines... if (unstableFlag) { // current stuff when unstable is active
Object.defineProperties(globalThis, unstableMethods);
Object.defineProperties(globalThis, unstableProperties);
Object.assign(denoNs, denoUnstableNs);
} else { // when in stable mode, throw if accessing an unstable key (prop/method)
/** things that should be disallowed, and the object it's for */
const disallowedUnstableKeys: [Object, string[]][] = [
[
globalThis, [
...Object.getOwnPropertyNames(unstableMethods),
...Object.getOwnPropertyNames(unstableProperties)
]
],
[ denoNs, Object.getOwnPropertyNames(denoUnstableNs) ]
];
for (const [ target, disallowedKeysForTarget ] of disallowedUnstableKeys) { // trying to avoid copy-pasting the same thing twice for different targets (globalThis and denoNs)
Object.defineProperties(
target,
disallowedKeysForTarget.reduce((obj, key) => { // for every unstable key, add a getter that will throw
return {
...obj,
[key]: {
get () { throw new Error(`Must use the --unstable flag to use '${key}'`); }
}
};
}, {})
);
}
} Adding this would probably mean that lib.deno.d.ts and lib.deno.unstable.d.ts would need to be merged so the compile can happen. P.S.: I've been trying/struggling to follow along with Deno's development, but is there a roadmap or something to help us identify what are the most important things to be working on? |
@lucacasonato I did think that we would want to rewrite I think this PRs solution is acceptable, though it might be challenging to maintain. It certainly is worth trying and potentially iterating on if it still causes chaos. |
@ferm10n problem with runtime exception is that user can catch and ignore it - and that's not desirable solution - unstable API errors should be terminal. |
@bartlomieju makes sense, but I would imagine that if a user is going to catch such an error, then they must have seen the error message "must use the --unstable flag" first. In any case, as soon as the user sees something I'm not sure it makes much sense for us to go to such lengths to protect the user from themselves if they are THAT determined to use things incorrectly. Even still, if we keep the compile time check, a determined enough user could still bypass it and try to use an unstable API without a terminal error if they access it like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lucacasonato please merge with master
.
This PR might be a hack, but it's a huge net improvement to current situation with multiple users being tripped by unhelpful diagnostics.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @lucacasonato
Ref #5175
cc @kitsonk is there a better way of transforming diagnostics? This feels kinda wrong.