Repository navigation
Custom type guard / generic predicate leaks outside its scope in certain settingsΒ #43719
Description
Activity
- changed the title
[-]Custom type guard / generic predicate leaks outside the bracket in certain settings[/-][+]Custom type guard / generic predicate leaks outside its scope in certain settings[/+]on Apr 17, 2021 Update: I mentioned earlier that the default
instance oftype guard worked in the playground but didn't work in my code. It turns out it was because the issue withinstance ofappears to be fixed in v4.3 So that's what I'm going to use, as I only had the custom type guards to get aroundinstance ofnot working.Nevertheless, the bug in question is still there.
To summarize this update:
if (item instance of NumClass) { // works perfectly in 4.3-beta in the complex example with 2-dimensional generic // dictionary. (didn't work at all in prior versions). } if (isNumClass(item)) { // custom type guard function // works partially in 4.2 and 4.3, with the issue that the type leaks outside // the block (this is what the bug report is about) // the issue only appears to occur in the complex example where there is // a generic that is a value of a 2-dimensional dictionary }Edit:
I didn't realize thatinstance ofloses the generic (e.g. it narrows toNumClass<number>, rather than toNumClass<T>). So the custom-type guard is still preferable.Edit2:
A workaround like this seems to work:
let item = this.slices[sliceId][sliceKey]; let itemAsNumClass = item; if (isNumClass(itemAsNumClass)) { itemAsNumClass.numExclusive(); } // now thanks to the variable alias, item's type is unaffected- addedNeeds InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.
on Apr 20, 2021 Using the variable (eg, adding an
item.get()after theifends), it looks like it's just the quickinfo that's wrong - our analysis proceeds as though it's still the base type (Slices[SliceId][SliceKey]), like we'd expect.- addedBugA bug in TypeScriptA bug in TypeScriptDomain: LS: Quick Infoe.g. hover text, tool-tips, and tooltips.e.g. hover text, tool-tips, and tooltips.and removedNeeds InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.
on Apr 21, 2021 Correction: Constraint locations (calls, accesses) perform correct analysis. Looks like we're incorrectly calculating the assignability of the nested indexes in the
falsebranch of the flow.- addedDomain: check: Control FlowThe issue relates to control flow analysisThe issue relates to control flow analysisDomain: Indexed Access TypesThe issue relates to accessing subtypes via index accessThe issue relates to accessing subtypes via index accessand removedDomain: LS: Quick Infoe.g. hover text, tool-tips, and tooltips.e.g. hover text, tool-tips, and tooltips.
on Apr 21, 2021 - addedFix AvailableA PR has been opened for this issueA PR has been opened for this issue
on Apr 21, 2021 - locked as resolved and limited conversation to collaborators
on Oct 21, 2025
Bug Report
π Search Terms
generic predicate custom type guard
π Version & Regression Information
Occurs in versions 4.2, 4.3 and nightly
In version 4.1 the custom type guard
isNumClassdidn't work at all insideComplexStore.getβ― Playground Link (full example)
Playground link with full code
π» Code (partial)
Small code sample (please see the playground link for full example).
π Actual behavior
π Expected behavior
I'm sorry if it's not a bug, but I think there are high chances it is. The one-dimensional example works ok now, and worked on in the previous version. The 2-dimensional example didn't work at all prior 4.2 and now works, but there are issues with it
By the way, I'm aware I could use bare
instance of, it appeared to work in this simplified example, but it didn't work in my real case (although I will continue to experiment with it).