Skip to content
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: 6 additions & 3 deletions src/nodes/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ export function isInConstContext(
}

// I believe we only need to check one level deep, regardless of how deep `node` is.
return isTransientSymbolLinksFlagSet(
(propertySymbol as ts.TransientSymbol).links,
ts.CheckFlags.Readonly,
return (
!!propertySymbol.links &&
isTransientSymbolLinksFlagSet(
(propertySymbol as ts.TransientSymbol).links,
Copy link
Collaborator

@RebeccaStevens RebeccaStevens Sep 27, 2025

Choose a reason for hiding this comment

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

Why do we need the as cast here but not 2 lines above?

ts.CheckFlags.Readonly,
)
);
}
case ts.SyntaxKind.PrefixUnaryExpression:
Expand Down
20 changes: 20 additions & 0 deletions src/types/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ describe("isPropertyReadonlyInType", () => {
),
).toBe(false);
});

it("does not crash when the property is inside a readonly array of a generic arrow function parameter", () => {
Copy link
Collaborator

@RebeccaStevens RebeccaStevens Sep 27, 2025

Choose a reason for hiding this comment

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

does not crash

That's a pretty low expectation.

const { sourceFile, typeChecker } = createSourceFileAndTypeChecker(`
declare const factory: <T>(x: readonly T[]) => (f: (x: T) => void) => void;

factory([{ abc: 42 }])((x) => { });
`);
const node = sourceFile.statements.at(-1) as ts.ExpressionStatement;
const call = node.expression as ts.CallExpression;
const parameter = call.arguments[0] as ts.ArrowFunction;
const type = typeChecker.getTypeAtLocation(parameter.parameters[0]);

expect(
isPropertyReadonlyInType(
type,
ts.escapeLeadingUnderscores("abc"),
typeChecker,
),
).toBe(false);
});
});

describe("symbolHasReadonlyDeclaration", () => {
Expand Down
Loading