Skip to content

Commit 5e21027

Browse files
author
Andy
authored
Reduce non-null assertions in getPropertySymbolsFromContextualType (#24675)
1 parent 7a79a45 commit 5e21027

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/services/findAllReferences.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,12 +1463,14 @@ namespace ts.FindAllReferences.Core {
14631463
}
14641464

14651465
/** Gets all symbols for one property. Does not get symbols for every property. */
1466-
function getPropertySymbolsFromContextualType(node: ObjectLiteralElement, checker: TypeChecker): ReadonlyArray<Symbol> {
1466+
function getPropertySymbolsFromContextualType(node: ObjectLiteralElementWithName, checker: TypeChecker): ReadonlyArray<Symbol> {
14671467
const contextualType = checker.getContextualType(<ObjectLiteralExpression>node.parent);
1468-
const name = getNameFromPropertyName(node.name!);
1469-
const symbol = contextualType && name && contextualType.getProperty(name);
1468+
if (!contextualType) return emptyArray;
1469+
const name = getNameFromPropertyName(node.name);
1470+
if (!name) return emptyArray;
1471+
const symbol = contextualType.getProperty(name);
14701472
return symbol ? [symbol] :
1471-
contextualType && contextualType.isUnion() ? mapDefined(contextualType.types, t => t.getProperty(name!)) : emptyArray; // TODO: GH#18217
1473+
contextualType.isUnion() ? mapDefined(contextualType.types, t => t.getProperty(name)) : emptyArray;
14721474
}
14731475

14741476
/**

src/services/services.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,21 +2186,23 @@ namespace ts {
21862186
* Returns the containing object literal property declaration given a possible name node, e.g. "a" in x = { "a": 1 }
21872187
*/
21882188
/* @internal */
2189-
export function getContainingObjectLiteralElement(node: Node): ObjectLiteralElement | undefined {
2189+
export function getContainingObjectLiteralElement(node: Node): ObjectLiteralElementWithName | undefined {
21902190
switch (node.kind) {
21912191
case SyntaxKind.StringLiteral:
21922192
case SyntaxKind.NumericLiteral:
21932193
if (node.parent.kind === SyntaxKind.ComputedPropertyName) {
2194-
return isObjectLiteralElement(node.parent.parent) ? node.parent.parent : undefined;
2194+
return isObjectLiteralElement(node.parent.parent) ? node.parent.parent as ObjectLiteralElementWithName : undefined;
21952195
}
21962196
// falls through
21972197
case SyntaxKind.Identifier:
21982198
return isObjectLiteralElement(node.parent) &&
21992199
(node.parent.parent.kind === SyntaxKind.ObjectLiteralExpression || node.parent.parent.kind === SyntaxKind.JsxAttributes) &&
2200-
node.parent.name === node ? node.parent : undefined;
2200+
node.parent.name === node ? node.parent as ObjectLiteralElementWithName : undefined;
22012201
}
22022202
return undefined;
22032203
}
2204+
/* @internal */
2205+
export type ObjectLiteralElementWithName = ObjectLiteralElement & { name: PropertyName };
22042206

22052207
/* @internal */
22062208
export function getPropertySymbolsFromContextualType(typeChecker: TypeChecker, node: ObjectLiteralElement): Symbol[] {

0 commit comments

Comments
 (0)