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

Fixed isReadonlySymbol check for intersection properties with .valueDeclaration #61346

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39007,8 +39007,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// Get accessors without matching set accessors
// Enum members
// Object.defineProperty assignments with writable false or no setter
// Unions and intersections of the above (unions and intersections eagerly set isReadonly on creation)
return !!(getCheckFlags(symbol) & CheckFlags.Readonly ||
// Unions and intersections of the above
const checkFlags = getCheckFlags(symbol);
if (checkFlags & CheckFlags.Synthetic) {
// unions and intersections eagerly compute Readonly flag on creation
return !!(checkFlags & CheckFlags.Readonly);
}
Comment on lines +39012 to +39015
Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. The issue arised because getDeclarationModifierFlagsFromSymbol(symbol) was returning ModifierFlags.Readonly based on the .valueDeclaration.
  2. .valueDeclaration is only set on union/intersection properties when it's "uniform"(ish). In this example, one source property symbol had a .valueDeclaration and the other one hadn't (the one from the mapped type hadn't it)
  3. but given that, as the existing comment states, the readonly flag is precomputed eagerly for union/intersections - this is really the only thing the compiler has to check in that situation. So I leveraged that for the fix here.

return !!(checkFlags & CheckFlags.Readonly ||
symbol.flags & SymbolFlags.Property && getDeclarationModifierFlagsFromSymbol(symbol) & ModifierFlags.Readonly ||
symbol.flags & SymbolFlags.Variable && getDeclarationNodeFlagsFromSymbol(symbol) & NodeFlags.Constant ||
symbol.flags & SymbolFlags.Accessor && !(symbol.flags & SymbolFlags.SetAccessor) ||
Expand Down
55 changes: 55 additions & 0 deletions tests/baselines/reference/intersectionTypeReadonly2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//// [tests/cases/conformance/types/intersection/intersectionTypeReadonly2.ts] ////

=== intersectionTypeReadonly2.ts ===
// https://github.com/microsoft/TypeScript/issues/61344

type A = { k: number };
>A : Symbol(A, Decl(intersectionTypeReadonly2.ts, 0, 0))
>k : Symbol(k, Decl(intersectionTypeReadonly2.ts, 2, 10))

type B = { readonly k: number };
>B : Symbol(B, Decl(intersectionTypeReadonly2.ts, 2, 23))
>k : Symbol(k, Decl(intersectionTypeReadonly2.ts, 3, 10))

(({}) as A & B).k = 0; // ok
>(({}) as A & B).k : Symbol(k, Decl(intersectionTypeReadonly2.ts, 2, 10), Decl(intersectionTypeReadonly2.ts, 3, 10))
>A : Symbol(A, Decl(intersectionTypeReadonly2.ts, 0, 0))
>B : Symbol(B, Decl(intersectionTypeReadonly2.ts, 2, 23))
>k : Symbol(k, Decl(intersectionTypeReadonly2.ts, 2, 10), Decl(intersectionTypeReadonly2.ts, 3, 10))

(({}) as B & A).k = 0; // ok
>(({}) as B & A).k : Symbol(k, Decl(intersectionTypeReadonly2.ts, 3, 10), Decl(intersectionTypeReadonly2.ts, 2, 10))
>B : Symbol(B, Decl(intersectionTypeReadonly2.ts, 2, 23))
>A : Symbol(A, Decl(intersectionTypeReadonly2.ts, 0, 0))
>k : Symbol(k, Decl(intersectionTypeReadonly2.ts, 3, 10), Decl(intersectionTypeReadonly2.ts, 2, 10))

type MakeWritable<T> = { -readonly [K in keyof T]: T[K] };
>MakeWritable : Symbol(MakeWritable, Decl(intersectionTypeReadonly2.ts, 6, 22))
>T : Symbol(T, Decl(intersectionTypeReadonly2.ts, 8, 18))
>K : Symbol(K, Decl(intersectionTypeReadonly2.ts, 8, 36))
>T : Symbol(T, Decl(intersectionTypeReadonly2.ts, 8, 18))
>T : Symbol(T, Decl(intersectionTypeReadonly2.ts, 8, 18))
>K : Symbol(K, Decl(intersectionTypeReadonly2.ts, 8, 36))

type C = MakeWritable<B>;
>C : Symbol(C, Decl(intersectionTypeReadonly2.ts, 8, 58))
>MakeWritable : Symbol(MakeWritable, Decl(intersectionTypeReadonly2.ts, 6, 22))
>B : Symbol(B, Decl(intersectionTypeReadonly2.ts, 2, 23))

(({}) as C).k = 0; // ok
>(({}) as C).k : Symbol(k, Decl(intersectionTypeReadonly2.ts, 3, 10))
>C : Symbol(C, Decl(intersectionTypeReadonly2.ts, 8, 58))
>k : Symbol(k, Decl(intersectionTypeReadonly2.ts, 3, 10))

(({}) as C & B).k = 0; // ok
>(({}) as C & B).k : Symbol(k, Decl(intersectionTypeReadonly2.ts, 3, 10), Decl(intersectionTypeReadonly2.ts, 3, 10))
>C : Symbol(C, Decl(intersectionTypeReadonly2.ts, 8, 58))
>B : Symbol(B, Decl(intersectionTypeReadonly2.ts, 2, 23))
>k : Symbol(k, Decl(intersectionTypeReadonly2.ts, 3, 10), Decl(intersectionTypeReadonly2.ts, 3, 10))

(({}) as B & C).k = 0; // ok
>(({}) as B & C).k : Symbol(k, Decl(intersectionTypeReadonly2.ts, 3, 10), Decl(intersectionTypeReadonly2.ts, 3, 10))
>B : Symbol(B, Decl(intersectionTypeReadonly2.ts, 2, 23))
>C : Symbol(C, Decl(intersectionTypeReadonly2.ts, 8, 58))
>k : Symbol(k, Decl(intersectionTypeReadonly2.ts, 3, 10), Decl(intersectionTypeReadonly2.ts, 3, 10))

115 changes: 115 additions & 0 deletions tests/baselines/reference/intersectionTypeReadonly2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//// [tests/cases/conformance/types/intersection/intersectionTypeReadonly2.ts] ////

=== intersectionTypeReadonly2.ts ===
// https://github.com/microsoft/TypeScript/issues/61344

type A = { k: number };
>A : A
> : ^
>k : number
> : ^^^^^^

type B = { readonly k: number };
>B : B
> : ^
>k : number
> : ^^^^^^

(({}) as A & B).k = 0; // ok
>(({}) as A & B).k = 0 : 0
> : ^
>(({}) as A & B).k : number
> : ^^^^^^
>(({}) as A & B) : A & B
> : ^^^^^
>({}) as A & B : A & B
> : ^^^^^
>({}) : {}
> : ^^
>{} : {}
> : ^^
>k : number
> : ^^^^^^
>0 : 0
> : ^

(({}) as B & A).k = 0; // ok
>(({}) as B & A).k = 0 : 0
> : ^
>(({}) as B & A).k : number
> : ^^^^^^
>(({}) as B & A) : B & A
> : ^^^^^
>({}) as B & A : B & A
> : ^^^^^
>({}) : {}
> : ^^
>{} : {}
> : ^^
>k : number
> : ^^^^^^
>0 : 0
> : ^

type MakeWritable<T> = { -readonly [K in keyof T]: T[K] };
>MakeWritable : MakeWritable<T>
> : ^^^^^^^^^^^^^^^

type C = MakeWritable<B>;
>C : MakeWritable<B>
> : ^^^^^^^^^^^^^^^

(({}) as C).k = 0; // ok
>(({}) as C).k = 0 : 0
> : ^
>(({}) as C).k : number
> : ^^^^^^
>(({}) as C) : MakeWritable<B>
> : ^^^^^^^^^^^^^^^
>({}) as C : MakeWritable<B>
> : ^^^^^^^^^^^^^^^
>({}) : {}
> : ^^
>{} : {}
> : ^^
>k : number
> : ^^^^^^
>0 : 0
> : ^

(({}) as C & B).k = 0; // ok
>(({}) as C & B).k = 0 : 0
> : ^
>(({}) as C & B).k : number
> : ^^^^^^
>(({}) as C & B) : MakeWritable<B> & B
> : ^^^^^^^^^^^^^^^^^^^
>({}) as C & B : MakeWritable<B> & B
> : ^^^^^^^^^^^^^^^^^^^
>({}) : {}
> : ^^
>{} : {}
> : ^^
>k : number
> : ^^^^^^
>0 : 0
> : ^

(({}) as B & C).k = 0; // ok
>(({}) as B & C).k = 0 : 0
> : ^
>(({}) as B & C).k : number
> : ^^^^^^
>(({}) as B & C) : B & MakeWritable<B>
> : ^^^^^^^^^^^^^^^^^^^
>({}) as B & C : B & MakeWritable<B>
> : ^^^^^^^^^^^^^^^^^^^
>({}) : {}
> : ^^
>{} : {}
> : ^^
>k : number
> : ^^^^^^
>0 : 0
> : ^

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

// https://github.com/microsoft/TypeScript/issues/61344

type A = { k: number };
type B = { readonly k: number };

(({}) as A & B).k = 0; // ok
(({}) as B & A).k = 0; // ok

type MakeWritable<T> = { -readonly [K in keyof T]: T[K] };

type C = MakeWritable<B>;
(({}) as C).k = 0; // ok
(({}) as C & B).k = 0; // ok
(({}) as B & C).k = 0; // ok