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

Narrow on element access of known property #10565

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ namespace ts {
case SyntaxKind.Identifier:
case SyntaxKind.ThisKeyword:
case SyntaxKind.PropertyAccessExpression:
case SyntaxKind.ElementAccessExpression:
return isNarrowableReference(expr);
case SyntaxKind.CallExpression:
return hasNarrowableArgument(<CallExpression>expr);
Expand All @@ -608,9 +609,20 @@ namespace ts {
}

function isNarrowableReference(expr: Expression): boolean {
return expr.kind === SyntaxKind.Identifier ||
expr.kind === SyntaxKind.ThisKeyword ||
expr.kind === SyntaxKind.PropertyAccessExpression && isNarrowableReference((<PropertyAccessExpression>expr).expression);
if (expr.kind === SyntaxKind.Identifier || expr.kind === SyntaxKind.ThisKeyword) {
return true;
}
if (expr.kind === SyntaxKind.PropertyAccessExpression) {
return isNarrowableReference((expr as PropertyAccessExpression).expression);
}
if (expr.kind === SyntaxKind.ElementAccessExpression) {
const access = expr as ElementAccessExpression;
const isArgumentLiteral = access.argumentExpression &&
(access.argumentExpression.kind === SyntaxKind.StringLiteral ||
access.argumentExpression.kind === SyntaxKind.NumericLiteral);
return isArgumentLiteral && isNarrowableReference(access.expression);
}
return false;
}

function hasNarrowableArgument(expr: CallExpression) {
Expand Down Expand Up @@ -1724,6 +1736,7 @@ namespace ts {
}
return checkStrictModeIdentifier(<Identifier>node);
case SyntaxKind.PropertyAccessExpression:
case SyntaxKind.ElementAccessExpression:
if (currentFlow && isNarrowableReference(<Expression>node)) {
node.flowNode = currentFlow;
}
Expand Down
52 changes: 36 additions & 16 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7854,11 +7854,25 @@ namespace ts {
case SyntaxKind.ThisKeyword:
return target.kind === SyntaxKind.ThisKeyword;
case SyntaxKind.PropertyAccessExpression:
return target.kind === SyntaxKind.PropertyAccessExpression &&
(<PropertyAccessExpression>source).name.text === (<PropertyAccessExpression>target).name.text &&
isMatchingReference((<PropertyAccessExpression>source).expression, (<PropertyAccessExpression>target).expression);
case SyntaxKind.ElementAccessExpression:
if (target.kind !== SyntaxKind.PropertyAccessExpression && target.kind !== SyntaxKind.ElementAccessExpression) {
return false;
}
const sourceAccess = source as PropertyAccessExpression | ElementAccessExpression;
const targetAccess = target as PropertyAccessExpression | ElementAccessExpression;
return isMatchingReference(sourceAccess.expression, targetAccess.expression) &&
getAccessedPropertyName(sourceAccess) === getAccessedPropertyName(targetAccess);
}
return false;
}

function getAccessedPropertyName(access: PropertyAccessExpression | ElementAccessExpression, type?: Type | undefined): string {
if (!type) {
type = getTypeOfNode(access.expression);
}
return false;
return access.kind === SyntaxKind.PropertyAccessExpression ?
(access as PropertyAccessExpression).name.text :
getPropertyNameForIndexedAccess((access as ElementAccessExpression).argumentExpression, type);
}

function containsMatchingReference(source: Node, target: Node) {
Expand Down Expand Up @@ -8353,7 +8367,9 @@ namespace ts {
type = narrowTypeBySwitchOnDiscriminant(type, flow.switchStatement, flow.clauseStart, flow.clauseEnd);
}
else if (isMatchingReferenceDiscriminant(expr)) {
type = narrowTypeByDiscriminant(type, <PropertyAccessExpression>expr, t => narrowTypeBySwitchOnDiscriminant(t, flow.switchStatement, flow.clauseStart, flow.clauseEnd));
type = narrowTypeByDiscriminant(type,
expr as PropertyAccessExpression | ElementAccessExpression,
t => narrowTypeBySwitchOnDiscriminant(t, flow.switchStatement, flow.clauseStart, flow.clauseEnd));
}
return createFlowType(type, isIncomplete(flowType));
}
Expand Down Expand Up @@ -8456,14 +8472,17 @@ namespace ts {
}

function isMatchingReferenceDiscriminant(expr: Expression) {
return expr.kind === SyntaxKind.PropertyAccessExpression &&
declaredType.flags & TypeFlags.Union &&
isMatchingReference(reference, (<PropertyAccessExpression>expr).expression) &&
isDiscriminantProperty(declaredType, (<PropertyAccessExpression>expr).name.text);
if (!(declaredType.flags & TypeFlags.Union) ||
expr.kind !== SyntaxKind.PropertyAccessExpression && expr.kind !== SyntaxKind.ElementAccessExpression) {
return false;
}
const access = expr as PropertyAccessExpression | ElementAccessExpression;
const name = getAccessedPropertyName(access, declaredType);
return isMatchingReference(reference, access.expression) && isDiscriminantProperty(declaredType, name);
}

function narrowTypeByDiscriminant(type: Type, propAccess: PropertyAccessExpression, narrowType: (t: Type) => Type): Type {
const propName = propAccess.name.text;
function narrowTypeByDiscriminant(type: Type, access: PropertyAccessExpression | ElementAccessExpression, narrowType: (t: Type) => Type): Type {
const propName = getAccessedPropertyName(access, type);
const propType = getTypeOfPropertyOfType(type, propName);
const narrowedPropType = propType && narrowType(propType);
return propType === narrowedPropType ? type : filterType(type, t => isTypeComparableTo(getTypeOfPropertyOfType(t, propName), narrowedPropType));
Expand All @@ -8474,7 +8493,7 @@ namespace ts {
return getTypeWithFacts(type, assumeTrue ? TypeFacts.Truthy : TypeFacts.Falsy);
}
if (isMatchingReferenceDiscriminant(expr)) {
return narrowTypeByDiscriminant(type, <PropertyAccessExpression>expr, t => getTypeWithFacts(t, assumeTrue ? TypeFacts.Truthy : TypeFacts.Falsy));
return narrowTypeByDiscriminant(type, <PropertyAccessExpression | ElementAccessExpression>expr, t => getTypeWithFacts(t, assumeTrue ? TypeFacts.Truthy : TypeFacts.Falsy));
}
if (containsMatchingReferenceDiscriminant(reference, expr)) {
return declaredType;
Expand Down Expand Up @@ -8506,10 +8525,10 @@ namespace ts {
return narrowTypeByEquality(type, operator, left, assumeTrue);
}
if (isMatchingReferenceDiscriminant(left)) {
return narrowTypeByDiscriminant(type, <PropertyAccessExpression>left, t => narrowTypeByEquality(t, operator, right, assumeTrue));
return narrowTypeByDiscriminant(type, <PropertyAccessExpression | ElementAccessExpression>left, t => narrowTypeByEquality(t, operator, right, assumeTrue));
}
if (isMatchingReferenceDiscriminant(right)) {
return narrowTypeByDiscriminant(type, <PropertyAccessExpression>right, t => narrowTypeByEquality(t, operator, left, assumeTrue));
return narrowTypeByDiscriminant(type, <PropertyAccessExpression | ElementAccessExpression>right, t => narrowTypeByEquality(t, operator, left, assumeTrue));
}
if (containsMatchingReferenceDiscriminant(reference, left) || containsMatchingReferenceDiscriminant(reference, right)) {
return declaredType;
Expand Down Expand Up @@ -10930,7 +10949,7 @@ namespace ts {
const prop = getPropertyOfType(objectType, name);
if (prop) {
getNodeLinks(node).resolvedSymbol = prop;
return getTypeOfSymbol(prop);
return getFlowTypeOfReference(node, getTypeOfSymbol(prop), /*assumeInitialized*/ true, /*flowContainer*/ undefined);
}
else if (isConstEnum) {
error(node.argumentExpression, Diagnostics.Property_0_does_not_exist_on_const_enum_1, name, symbolToString(objectType.symbol));
Expand Down Expand Up @@ -14030,7 +14049,8 @@ namespace ts {
for (const decl of indexSymbol.declarations) {
const declaration = <SignatureDeclaration>decl;
if (declaration.parameters.length === 1 && declaration.parameters[0].type) {
switch (declaration.parameters[0].type.kind) {
const workaround = 0;
switch (declaration.parameters[workaround].type.kind) {
case SyntaxKind.StringKeyword:
if (!seenStringIndexer) {
seenStringIndexer = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//// [typeGuardNarrowsIndexedAccessOfKnownProperty.ts]
interface Square {
kind: "square";
size: number;
}

interface Rectangle {
kind: "rectangle";
width: number;
height: number;
}

interface Circle {
kind: "circle";
radius: number;
}

type Shape = Square | Rectangle | Circle;
interface Subshape {
"0": {
sub: {
under: {
shape: Shape;
}
}
}
}
function area(s: Shape): number {
switch(s['kind']) {
case "square": return s.size * s.size;
case "rectangle": return s.width * s.height;
case "circle": return Math.PI * s.radius * s.radius;
}
}

function subarea(s: Subshape): number {
switch(s[0]["sub"].under["shape"]["kind"]) {
case "square": return s[0].sub.under.shape.size * s[0].sub.under.shape.size;
case "rectangle": return s[0]["sub"]["under"]["shape"]["width"] * s[0]["sub"]["under"]["shape"].height;
case "circle": return Math.PI * s[0].sub.under["shape"].radius * s[0]["sub"].under.shape["radius"];
}
}


//// [typeGuardNarrowsIndexedAccessOfKnownProperty.js]
function area(s) {
switch (s['kind']) {
case "square": return s.size * s.size;
case "rectangle": return s.width * s.height;
case "circle": return Math.PI * s.radius * s.radius;
}
}
function subarea(s) {
switch (s[0]["sub"].under["shape"]["kind"]) {
case "square": return s[0].sub.under.shape.size * s[0].sub.under.shape.size;
case "rectangle": return s[0]["sub"]["under"]["shape"]["width"] * s[0]["sub"]["under"]["shape"].height;
case "circle": return Math.PI * s[0].sub.under["shape"].radius * s[0]["sub"].under.shape["radius"];
}
}
Loading