-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Type correlation regression #54834
Comments
The change between origin/release-5.0 and origin/release-5.1 occurred at 9769421. |
So it seems that this fixes it #54689 but I'm not entirely sure if that solution is on the right track. |
I think this somewhat accidentally worked with type TypeMap = {
NumericLiteral: { value: number },
StringLiteral: { value: string },
Identifier: { name: string },
CallExpression: { name: string, arguments: DropbearNode[] }
};
type DropbearNode<K extends keyof TypeMap = keyof TypeMap> = { [P in K]: { type: P } & TypeMap[P] }[K];
type NumericLiteral = DropbearNode<"NumericLiteral">;
type StringLiteral = DropbearNode<"StringLiteral">;
type Identifier = DropbearNode<"Identifier">;
type CallExpression = DropbearNode<"CallExpression">;
type Visitor = {
[K in keyof TypeMap]: (node: DropbearNode<K>) => void;
};
function visitNode<K extends keyof TypeMap>(node: DropbearNode<K>, v: Visitor) {
v[node.type](node);
} |
This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
@ahejlsberg yes that solution works nicely until we need to apply modifiers to the type of |
It works if |
Bug Report
🔎 Search Terms
indexed Access Inference Improvements, indexed access type, mapped type, correlated types
🕗 Version & Regression Information
When did you start seeing this bug occur?
Without
Readonly
it never worked, the compiler doesn't see the correlation betweenv[node.type]
andnode
.With
Readonly
stopped working from5.1.3
.⏯ Playground Link
With
Readonly
Without
Readonly
💻 Code
🙁 Actual behavior
The focus is the implementation of the
visitNode
function .Without the use of
Readonly
in the definition ofVisitor
andvisitNode
the code does not compile in any version of TS. On the other hand, usingReadonly
in those definitions has worked until5.1.3
. Thanks to thoseReadonly
TS was somehow able to see the correlation betweenv[node.type]
andnode
.P.S. It seems that
v[node.type as K](node)
always solves the problem, in all versions, with or without the use ofReadonly
.🙂 Expected behavior
I repropose a simplified version of this example because I've recently had a chance to get my hands back on code that makes use of the pattern suggested in #47109. This is sort of a simplified version of what is suggested in that PR so I am not sure about the right behaviour.
It would be nice if TS would be able to support this simplified pattern for expressing correlations, instead of something more convoluted like this one where I think I am perfectly following what is indicated in #47109. And TS seems to be able to do it, although I don't know why
Readonly
was needed nor why it suddenly stopped working.The text was updated successfully, but these errors were encountered: