Skip to content
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

Do not widen computed properties with template literal types #54706

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Andarist
Copy link
Contributor

@Andarist Andarist commented Jun 19, 2023

addresses @jcalz's comment here: #13948 (comment) , it's related to the primary issue there ( #13948 ) but it doesn't address "union lifting". There was once a PR that addressed this ( #21070 ) but it wasn't merged (despite being approved). I could revive that work but I'm unsure what exactly was wrong with that PR so I'm not sure what could be prepared by me that would have any chance of being merged

improves #46309

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Jun 19, 2023
@typescript-bot
Copy link
Collaborator

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

@Andarist Andarist force-pushed the dont-widen-computed-prop-wiht-patterns branch from e7172a2 to c2c5ae1 Compare June 19, 2023 14:54
src/compiler/checker.ts Outdated Show resolved Hide resolved
@Andarist Andarist force-pushed the dont-widen-computed-prop-wiht-patterns branch from c2c5ae1 to 275410d Compare June 19, 2023 15:10
Comment on lines +97 to +100
const doc_46309: IDocument_46309 = {
name: "",
[`added_${tech1_46309.uuid}`]: [19700101],
};
Copy link
Contributor Author

@Andarist Andarist Jun 19, 2023

Choose a reason for hiding this comment

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

This still doesn't work. I think that it should but that's perhaps a different issue to be solved - properties like this could not widen based on the contextual type (I'm not yet sure how complex that could get with multiple index signatures in the target and so on).

The second example with [`added_${tech1_46309.uuid}` as const] is something new that works now though.

};

const obj13 = {
>obj13 : {}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's worth noting here that this doesn't preserve any index signatures because getUnionIndexInfos([left, right]) in getSpreadType doesn't preserve any index signatures that are not common to all of the input types. That feels like a separate issue to be solved - if it is meant to be solved.

@sandersn sandersn added this to Not started in PR Backlog Jun 29, 2023
@sandersn sandersn moved this from Not started to Waiting on reviewers in PR Backlog Jul 19, 2023
Copy link
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

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

I could revive that work but I'm unsure what exactly was wrong with that PR so I'm not sure what I could be prepared by me that would have any chance of being merged

Originally (it's very old), it opened a can of worms regarding things we could and could not represent as property names and index signatures. Now we have pattern literal index signatures and things are generally better (though the union case is a bit muddier still, imo, in theory we could handle it pretty well now). The real sticking point, then and now, is going to be the generic types used in these positions. If you feed a T extends string into a get${T} in a computed name position, how's that supposed to work? IMO, our index signature logic should be robust enough to handle generic index signature keys (or is very close to being robust enough to handle them), but @ahejlsberg has had philosophical issues with the concept in the past - preferring that mapped types remain "the one true way" to make "generic keys" exist (and, well, that being the case likely does simplify how we handle index signatures and property names in some places!)... the followup, thus, if that is still the case, is that maybe having a generic in a computed name position should get you an intersection with a generic Record mapped type that can defer the property creation for you. At least I think that's how this'd have to be extended to work with generics/unions, based on what I know.

In any case, the literal-y-ness part of this is almost definitely welcome. Definitely get some generic pattern literals in computed name positions into the tests for this PR, though, since we want to verify those still just give string index signatures for now, since we don't really have a better behavior for that down.

if (isTypeAssignableTo(computedNameType, stringNumberSymbolType)) {
if (computedNameType && !isTypeUsableAsPropertyName(computedNameType)) {
if (isPatternLiteralType(computedNameType)) {
indexKeyTypes.add(computedNameType);
Copy link
Member

Choose a reason for hiding this comment

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

This probably warrants the "breaking change" tag, since previously these'd make a string index signature get added, but now you get a more specific index signature. Which is, I believe, the goal of the change, it's just... maybe breaky? It's unclear.

Copy link
Member

Choose a reason for hiding this comment

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

Also there's the whole union thing - but the union thing can be a problem for another day.

PR Backlog automation moved this from Waiting on reviewers to Waiting on author Nov 9, 2023
@weswigham weswigham added the Breaking Change Would introduce errors in existing code label Nov 9, 2023
@Andarist
Copy link
Contributor Author

@weswigham if I understand correctly the only requested change here was to add a generic test case and I've just pushed out one. Could you take another look?

…rop-wiht-patterns

# Conflicts:
#	tests/baselines/reference/indexSignatures1.types
@Andarist Andarist force-pushed the dont-widen-computed-prop-wiht-patterns branch from e0df494 to 1a97927 Compare May 3, 2024 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking Change Would introduce errors in existing code For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Status: Waiting on author
PR Backlog
  
Waiting on author
Development

Successfully merging this pull request may close these issues.

None yet

3 participants