Skip to content

Prevent substitution types from leaking out of non-deferred conditionals - #54894

Closed
Mateusz Burzyński (Andarist) wants to merge 2 commits into
microsoft:mainfrom
Andarist:fix/substition-as-index
Closed

Prevent substitution types from leaking out of non-deferred conditionals#54894
Mateusz Burzyński (Andarist) wants to merge 2 commits into
microsoft:mainfrom
Andarist:fix/substition-as-index

Conversation

@Andarist

Copy link
Copy Markdown
Contributor

fixes#54886

@typescript-botTypeScript Bot (typescript-bot) added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Jul 6, 2023
@AndaristMateusz Burzyński (Andarist) changed the title Fixed an issue with substitution type not always being usable as index typePrevent substitution types from leaking non-deferred conditionalsJul 6, 2023
@AndaristMateusz Burzyński (Andarist) changed the title Prevent substitution types from leaking non-deferred conditionalsPrevent substitution types from leaking out of non-deferred conditionalsJul 6, 2023
@typescript-botTypeScript Bot (typescript-bot) added For Backlog Bug PRs that fix a backlog bug and removed For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Jul 19, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't quite feel like the right fix - I'm actually OK with substitution types exiting conditional boundaries, it's useful in many ways - in fact, it's sometimes the only way chained conditionals like SplitString<Cast<T, string>> typecheck. Naturally, using the intersection means these cases will still work... but the ux when you see these "narrowed" types will get a bit (ok, a lot) worse.

Honestly, a much better fix is noting that in getPropertyTypeForIndexType we use isTypeAssignableToKind to make these errors, which is... not great (any type-kind based analysis is suspect, given how generics work). But so long as we are, updating isTypeAssignableToKind to intersect substitutions is probably ok.

}
if (result.flags & TypeFlags.Substitution) {
const substitution = result as SubstitutionType;
result = getIntersectionType([substitution.baseType, substitution.constraint]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use getSubstitutionIntersection, if this sticks around.

@Andarist

Copy link
Copy Markdown
ContributorAuthor

superseded by #57113

@microsoftMicrosoft (microsoft) locked as resolved and limited conversation to collaborators Oct 16, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

For Backlog BugPRs that fix a backlog bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

extends causing string literal can't be used as index

4 participants

@Andarist@weswigham@sandersn@typescript-bot