-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Resolve keyof and index operations instead of their targets. #58758
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
Resolve keyof and index operations instead of their targets. #58758
Conversation
@@ -11644,6 +11644,14 @@ export function unwrapParenthesizedExpression(o: Expression) { | |||
return o; | |||
} | |||
|
|||
/** @internal */ | |||
export function unwrapParenthesizedType(o: TypeNode) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm surprised we don't already have this helper...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was surprised too. But I could not find it if it exists. There is one that walks up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, it's skipTypeParentheses
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh. Didn't think to look for that. I'll fix it tomorrow.
src/compiler/checker.ts
Outdated
function tryVisitIndexAccess(node: IndexedAccessTypeNode): TypeNode | undefined { | ||
const resultObjectType = tryVisitSimpleTypeNode(node.objectType); | ||
if (resultObjectType === undefined) { | ||
return undefined; | ||
} | ||
return factory.updateIndexedAccessTypeNode(node, resultObjectType, visitNode(node.indexType, visitExistingNodeTreeSymbols, isTypeNode)!); | ||
} | ||
function tryVisitKeyOf(node: TypeOperatorNode): TypeNode | undefined { | ||
Debug.assertEqual(node.operator, SyntaxKind.KeyOfKeyword); | ||
const type = tryVisitSimpleTypeNode(node.type); | ||
if (type === undefined) { | ||
return undefined; | ||
} | ||
return factory.updateTypeOperatorNode(node, type); | ||
} | ||
function tryVisitTypeQuery(node: TypeQueryNode): TypeNode | undefined { | ||
const { introducesError, node: exprName } = trackExistingEntityName(node.exprName, context); | ||
if (!introducesError) { | ||
return factory.updateTypeQueryNode( | ||
node, | ||
exprName, | ||
visitNodes(node.typeArguments, visitExistingNodeTreeSymbols, isTypeNode), | ||
); | ||
} | ||
|
||
const serializedName = serializeTypeName(context, node.exprName, /*isTypeOf*/ true); | ||
if (serializedName) { | ||
return setTextRange(context, serializedName, node.exprName); | ||
} | ||
} | ||
function tryVisitTypeReference(node: TypeReferenceNode): TypeNode | undefined { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function tryVisitIndexAccess(node: IndexedAccessTypeNode): TypeNode | undefined { | |
const resultObjectType = tryVisitSimpleTypeNode(node.objectType); | |
if (resultObjectType === undefined) { | |
return undefined; | |
} | |
return factory.updateIndexedAccessTypeNode(node, resultObjectType, visitNode(node.indexType, visitExistingNodeTreeSymbols, isTypeNode)!); | |
} | |
function tryVisitKeyOf(node: TypeOperatorNode): TypeNode | undefined { | |
Debug.assertEqual(node.operator, SyntaxKind.KeyOfKeyword); | |
const type = tryVisitSimpleTypeNode(node.type); | |
if (type === undefined) { | |
return undefined; | |
} | |
return factory.updateTypeOperatorNode(node, type); | |
} | |
function tryVisitTypeQuery(node: TypeQueryNode): TypeNode | undefined { | |
const { introducesError, node: exprName } = trackExistingEntityName(node.exprName, context); | |
if (!introducesError) { | |
return factory.updateTypeQueryNode( | |
node, | |
exprName, | |
visitNodes(node.typeArguments, visitExistingNodeTreeSymbols, isTypeNode), | |
); | |
} | |
const serializedName = serializeTypeName(context, node.exprName, /*isTypeOf*/ true); | |
if (serializedName) { | |
return setTextRange(context, serializedName, node.exprName); | |
} | |
} | |
function tryVisitTypeReference(node: TypeReferenceNode): TypeNode | undefined { | |
function tryVisitIndexAccess(node: IndexedAccessTypeNode): TypeNode | undefined { | |
const resultObjectType = tryVisitSimpleTypeNode(node.objectType); | |
if (resultObjectType === undefined) { | |
return undefined; | |
} | |
return factory.updateIndexedAccessTypeNode(node, resultObjectType, visitNode(node.indexType, visitExistingNodeTreeSymbols, isTypeNode)!); | |
} | |
function tryVisitKeyOf(node: TypeOperatorNode): TypeNode | undefined { | |
Debug.assertEqual(node.operator, SyntaxKind.KeyOfKeyword); | |
const type = tryVisitSimpleTypeNode(node.type); | |
if (type === undefined) { | |
return undefined; | |
} | |
return factory.updateTypeOperatorNode(node, type); | |
} | |
function tryVisitTypeQuery(node: TypeQueryNode): TypeNode | undefined { | |
const { introducesError, node: exprName } = trackExistingEntityName(node.exprName, context); | |
if (!introducesError) { | |
return factory.updateTypeQueryNode( | |
node, | |
exprName, | |
visitNodes(node.typeArguments, visitExistingNodeTreeSymbols, isTypeNode), | |
); | |
} | |
const serializedName = serializeTypeName(context, node.exprName, /*isTypeOf*/ true); | |
if (serializedName) { | |
return setTextRange(context, serializedName, node.exprName); | |
} | |
} | |
function tryVisitTypeReference(node: TypeReferenceNode): TypeNode | undefined { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just newlines between the declarations
} | ||
return visitNode(node, visitExistingNodeTreeSymbols, isTypeNode); | ||
} | ||
function tryVisitIndexAccess(node: IndexedAccessTypeNode): TypeNode | undefined { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tryVisitIndexedAccess
Seems reasonable. Can you just address the style nits? I don't have edit access. |
@typescript-bot cherry-pick to release-5.5 |
Hey, @DanielRosenwasser! I've created #58767 for you. |
…e-5.5 (#58767) Co-authored-by: Titian Cernicova-Dragomir <[email protected]> Co-authored-by: Daniel Rosenwasser <[email protected]>
Fixes #58752