Closed marekdedic closed 4 months ago
Minimal repro case with JS file (TS playground):
'use strict';
/** @type {globalThis['structuredClone']} */
const structuredClone =
globalThis.structuredClone ??
function structuredClone (value, options = undefined) {
if (arguments.length === 0) {
throw new TypeError('missing argument')
}
return value;
}
The problem is related to the synthetic rest parameter that is added by maybeAddJsSyntheticRestParameter
.
There is a comment there that this deferred type is meant to be overridable by contextual typing if it's not locked by getTypeOfSymbol
before it gets to assigning contextual types. However, exactly this happens here. checkGrammarParameterList
calls isOptionalParameter
, that calls getMinArgumentCount
and that calls getTypeOfSymbol
.
So likely that comment doesn't hold true at all. It's likely that this type is requested at all times and it's never overridden by the contextual type in practice. I think that getMinArgumentCount
can recognize this symbol as special and avoid requesting this type there. That would fix the issue.
There is an extra potential issue here though. As I mentioned, that code comment implies that the contextual type might not override the type of this synthetic symbol if its type gets requested before it gets to that. That implies that it just shouldn't throw at all in such a situation as it's implied that it's possible. Yet it is a hard - unconditional - crash if that ever happens.
An extra thing about this specific case is that maybe containsArgumentsReference
wouldn't even have to return true
here.
Either way, I'll give this more thought and put up a PR with the fix some time later.
Seeing the related activity in the PRs, I just want to note that the issue persists for me even with TS 5.4.4. Don't know if that's any help... Thanks!
The PR that fixed it (https://github.com/microsoft/TypeScript/pull/57580) is not part of the 5.4 release - it has landed on main
though.
The issue is fixed on 5.5, thanks!
π Search Terms
Debug Failure. Expected [object Object] === [object Object]. Parameter symbol already has a cached type which differs from newly assigned type
π Version & Regression Information
β― Playground Link
https://github.com/skaut/wordpress-version-checker/tree/typescript-chached-type-issue
π» Code
See https://github.com/skaut/wordpress-version-checker/tree/typescript-chached-type-issue
Run
npm ci && npm test
Sorry for not providing a playground link, but I could only replicate this with Jest involved
π Actual behavior
π Expected behavior
No TS error - the test should run
Additional information about the issue
No response