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 down non-const enity name expressions within element access expressions #56392

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
31 changes: 7 additions & 24 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2834,8 +2834,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return findAncestor(errorBindingElement, isBindingElement) !== findAncestor(declaration, isBindingElement) ||
declaration.pos < errorBindingElement.pos;
}
const rootDeclaration = getRootDeclaration(declaration) as Declaration;
if (rootDeclaration.kind !== SyntaxKind.VariableDeclaration) {
return true;
}
Comment on lines +2838 to +2840
Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO: recheck correctness here

// or it might be illegal if usage happens before parent variable is declared (eg var [a] = a)
return isBlockScopedNameDeclaredBeforeUse(getAncestor(declaration, SyntaxKind.VariableDeclaration) as Declaration, usage);
return isBlockScopedNameDeclaredBeforeUse(rootDeclaration, usage);
}
else if (declaration.kind === SyntaxKind.VariableDeclaration) {
// still might be illegal if usage is in the initializer of the variable declaration (eg var a = a)
Expand Down Expand Up @@ -26404,29 +26408,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

function tryGetNameFromEntityNameExpression(node: EntityNameOrEntityNameExpression) {
const symbol = resolveEntityName(node, SymbolFlags.Value, /*ignoreErrors*/ true);
if (!symbol || !(isConstantVariable(symbol) || (symbol.flags & SymbolFlags.EnumMember))) return undefined;

const declaration = symbol.valueDeclaration;
if (declaration === undefined) return undefined;

const type = tryGetTypeFromEffectiveTypeNode(declaration);
if (type) {
const name = tryGetNameFromType(type);
if (name !== undefined) {
return name;
}
}
if (hasOnlyExpressionInitializer(declaration) && isBlockScopedNameDeclaredBeforeUse(declaration, node)) {
const initializer = getEffectiveInitializer(declaration);
if (initializer) {
const initializerType = isBindingPattern(declaration.parent) ? getTypeForBindingElement(declaration as BindingElement) : getTypeOfExpression(initializer);
return initializerType && tryGetNameFromType(initializerType);
}
if (isEnumMember(declaration)) {
return getTextOfPropertyName(declaration.name);
}
}
return undefined;
if (!symbol || symbol.valueDeclaration && !isBlockScopedNameDeclaredBeforeUse(symbol.valueDeclaration, node)) return undefined;
return tryGetNameFromType(getTypeOfExpression(node as Expression));
}

function containsMatchingReference(source: Node, target: Node) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [tests/cases/compiler/typeGuardNarrowsIndexedAccessOfKnownProperty13.ts] ////

=== typeGuardNarrowsIndexedAccessOfKnownProperty13.ts ===
interface Data {
>Data : Symbol(Data, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty13.ts, 0, 0))

a?: number;
>a : Symbol(Data.a, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty13.ts, 0, 16))
}

declare const data: Data;
>data : Symbol(data, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty13.ts, 4, 13))
>Data : Symbol(Data, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty13.ts, 0, 0))

let key = "a" as const;
>key : Symbol(key, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty13.ts, 6, 3))
>const : Symbol(const)

if (data.a !== undefined) {
>data.a : Symbol(Data.a, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty13.ts, 0, 16))
>data : Symbol(data, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty13.ts, 4, 13))
>a : Symbol(Data.a, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty13.ts, 0, 16))
>undefined : Symbol(undefined)

const a = data[key];
>a : Symbol(a, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty13.ts, 9, 7))
>data : Symbol(data, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty13.ts, 4, 13))
>key : Symbol(key, Decl(typeGuardNarrowsIndexedAccessOfKnownProperty13.ts, 6, 3))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [tests/cases/compiler/typeGuardNarrowsIndexedAccessOfKnownProperty13.ts] ////

=== typeGuardNarrowsIndexedAccessOfKnownProperty13.ts ===
interface Data {
a?: number;
>a : number | undefined
}

declare const data: Data;
>data : Data

let key = "a" as const;
>key : "a"
>"a" as const : "a"
>"a" : "a"

if (data.a !== undefined) {
>data.a !== undefined : boolean
>data.a : number | undefined
>data : Data
>a : number | undefined
>undefined : undefined

const a = data[key];
>a : number
>data[key] : number
>data : Data
>key : "a"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @strict: true
// @noEmit: true

interface Data {
a?: number;
}

declare const data: Data;

let key = "a" as const;

if (data.a !== undefined) {
const a = data[key];
}
Loading