Skip to content
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
7 changes: 7 additions & 0 deletions internal/checker/pseudotypenodebuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,13 @@ func (b *NodeBuilderImpl) pseudoTypeEquivalentToType(t *pseudochecker.PseudoType
return false
}
tupleTarget := type_.TargetTupleType()
// Pseudo-tuples always emit as readonly (see pseudoTypeToNode), so they can only match
// readonly tuples. When contextual typing makes the tuple non-readonly (e.g., via
// `as const satisfies T` where T has a mutable array), we must fall back to the
// regular type serialization path which correctly handles the readonly flag. (#3192)
if !tupleTarget.readonly {
return false
}
// Pseudo-tuples come from `as const` array literals, so they only ever have required elements.
// If the target tuple has optional, rest, or variadic elements, the structures can't match.
if tupleTarget.combinedFlags&ElementFlagsNonRequired != 0 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//// [tests/cases/compiler/declarationEmitReadonlyTupleFromConstSatisfies.ts] ////

//// [declarationEmitReadonlyTupleFromConstSatisfies.ts]
// Regression test for #3192
// `as const satisfies` with a mutable array contextual type should not
// emit a readonly tuple in declaration output.

export const obj = {
array: [
{ n: 1 },
{ n: 2 },
],
} as const satisfies { array?: Readonly<{ n: unknown; }>[] }


//// [declarationEmitReadonlyTupleFromConstSatisfies.js]
// Regression test for #3192
// `as const satisfies` with a mutable array contextual type should not
// emit a readonly tuple in declaration output.
export const obj = {
array: [
{ n: 1 },
{ n: 2 },
],
};


//// [declarationEmitReadonlyTupleFromConstSatisfies.d.ts]
export declare const obj: {
readonly array: [{
readonly n: 1;
}, {
readonly n: 2;
}];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [tests/cases/compiler/declarationEmitReadonlyTupleFromConstSatisfies.ts] ////

=== declarationEmitReadonlyTupleFromConstSatisfies.ts ===
// Regression test for #3192
// `as const satisfies` with a mutable array contextual type should not
// emit a readonly tuple in declaration output.

export const obj = {
>obj : Symbol(obj, Decl(declarationEmitReadonlyTupleFromConstSatisfies.ts, 4, 12))

array: [
>array : Symbol(array, Decl(declarationEmitReadonlyTupleFromConstSatisfies.ts, 4, 20))

{ n: 1 },
>n : Symbol(n, Decl(declarationEmitReadonlyTupleFromConstSatisfies.ts, 6, 5))

{ n: 2 },
>n : Symbol(n, Decl(declarationEmitReadonlyTupleFromConstSatisfies.ts, 7, 5))

],
} as const satisfies { array?: Readonly<{ n: unknown; }>[] }
>const : Symbol(const)
>array : Symbol(array, Decl(declarationEmitReadonlyTupleFromConstSatisfies.ts, 9, 22))
>Readonly : Symbol(Readonly, Decl(lib.es5.d.ts, --, --))
>n : Symbol(n, Decl(declarationEmitReadonlyTupleFromConstSatisfies.ts, 9, 41))

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

=== declarationEmitReadonlyTupleFromConstSatisfies.ts ===
// Regression test for #3192
// `as const satisfies` with a mutable array contextual type should not
// emit a readonly tuple in declaration output.

export const obj = {
>obj : { readonly array: [{ readonly n: 1; }, { readonly n: 2; }]; }
>{ array: [ { n: 1 }, { n: 2 }, ],} as const satisfies { array?: Readonly<{ n: unknown; }>[] } : { readonly array: [{ readonly n: 1; }, { readonly n: 2; }]; }
>{ array: [ { n: 1 }, { n: 2 }, ],} as const : { readonly array: [{ readonly n: 1; }, { readonly n: 2; }]; }
>{ array: [ { n: 1 }, { n: 2 }, ],} : { readonly array: [{ readonly n: 1; }, { readonly n: 2; }]; }

array: [
>array : [{ readonly n: 1; }, { readonly n: 2; }]
>[ { n: 1 }, { n: 2 }, ] : [{ readonly n: 1; }, { readonly n: 2; }]

{ n: 1 },
>{ n: 1 } : { readonly n: 1; }
>n : 1
>1 : 1

{ n: 2 },
>{ n: 2 } : { readonly n: 2; }
>n : 2
>2 : 2

],
} as const satisfies { array?: Readonly<{ n: unknown; }>[] }
>array : Readonly<{ n: unknown; }>[] | undefined
>n : unknown

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

// Regression test for #3192
// `as const satisfies` with a mutable array contextual type should not
// emit a readonly tuple in declaration output.

export const obj = {
array: [
{ n: 1 },
{ n: 2 },
],
} as const satisfies { array?: Readonly<{ n: unknown; }>[] }
Loading