Skip to content

Commit

Permalink
fix(57635): duplicate property name error when trying to overwrite ea…
Browse files Browse the repository at this point in the history
…rly-bound prop with late-bound prop (#57717)
  • Loading branch information
idango10 authored Mar 26, 2024
1 parent 088f25a commit 316f180
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
6 changes: 2 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13073,12 +13073,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
let lateSymbol = lateSymbols.get(memberName);
if (!lateSymbol) lateSymbols.set(memberName, lateSymbol = createSymbol(SymbolFlags.None, memberName, CheckFlags.Late));

// Report an error if a late-bound member has the same name as an early-bound member,
// or if we have another early-bound symbol declaration with the same name and
// conflicting flags.
// Report an error if there's a symbol declaration with the same name and conflicting flags.
const earlySymbol = earlySymbols && earlySymbols.get(memberName);
// Duplicate property declarations of classes are checked in checkClassForDuplicateDeclarations.
if (!(parent.flags & SymbolFlags.Class) && (lateSymbol.flags & getExcludedSymbolFlags(symbolFlags) || earlySymbol)) {
if (!(parent.flags & SymbolFlags.Class) && lateSymbol.flags & getExcludedSymbolFlags(symbolFlags)) {
// If we have an existing early-bound member, combine its declarations so that we can
// report an error at each declaration.
const declarations = earlySymbol ? concatenate(earlySymbol.declarations, lateSymbol.declarations) : lateSymbol.declarations;
Expand Down
8 changes: 1 addition & 7 deletions tests/baselines/reference/dynamicNamesErrors.errors.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
dynamicNamesErrors.ts(5,5): error TS2718: Duplicate property '1'.
dynamicNamesErrors.ts(6,5): error TS2733: Property '1' was also declared here.
dynamicNamesErrors.ts(19,5): error TS2717: Subsequent property declarations must have the same type. Property '[c1]' must be of type 'number', but here has type 'string'.
dynamicNamesErrors.ts(24,1): error TS2322: Type 'T2' is not assignable to type 'T1'.
Types of property '[c0]' are incompatible.
Expand All @@ -9,17 +7,13 @@ dynamicNamesErrors.ts(25,1): error TS2322: Type 'T1' is not assignable to type '
Type 'number' is not assignable to type 'string'.


==== dynamicNamesErrors.ts (5 errors) ====
==== dynamicNamesErrors.ts (3 errors) ====
const c0 = "1";
const c1 = 1;

interface T0 {
[c0]: number;
~~~~
!!! error TS2718: Duplicate property '1'.
1: number;
~
!!! error TS2733: Property '1' was also declared here.
}

interface T1 {
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/dynamicNamesErrors.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ interface T0 {
>T0 : Symbol(T0, Decl(dynamicNamesErrors.ts, 1, 13))

[c0]: number;
>[c0] : Symbol(T0[c0], Decl(dynamicNamesErrors.ts, 3, 14))
>[c0] : Symbol(T0[1], Decl(dynamicNamesErrors.ts, 4, 17), Decl(dynamicNamesErrors.ts, 3, 14))
>c0 : Symbol(c0, Decl(dynamicNamesErrors.ts, 0, 5))

1: number;
>1 : Symbol(T0[1], Decl(dynamicNamesErrors.ts, 4, 17))
>1 : Symbol(T0[1], Decl(dynamicNamesErrors.ts, 4, 17), Decl(dynamicNamesErrors.ts, 3, 14))
}

interface T1 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path="fourslash.ts"/>

//// export {};

//// const prop = "abc";

//// function foo(): void {};
//// foo.abc = 10;
//// foo[prop] = 10;

//// interface T0 {
//// [prop]: number;
//// abc: number;
//// }

verify.noErrors();

0 comments on commit 316f180

Please sign in to comment.