Skip to content

Commit

Permalink
Merge pull request #18 from JoshuaKGoldberg/readonly-modifiers-check
Browse files Browse the repository at this point in the history
fix: account for mapped types without modifiers in isPropertyReadonlyInType
  • Loading branch information
JoshuaKGoldberg committed Feb 15, 2023
2 parents c5bb9fa + d64c9be commit 405c475
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
26 changes: 26 additions & 0 deletions src/types/utilities.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as ts from "typescript";
import { describe, expect, it } from "vitest";

import { createSourceFileAndTypeChecker } from "../test/utils";
import { isPropertyReadonlyInType } from "./utilities";

describe("isPropertyReadonlyInType", () => {
it("does not crash when the type is a mapped type parameter extending any", () => {
const { sourceFile, typeChecker } = createSourceFileAndTypeChecker(`
type MyType<T> = {
[K in keyof T]: 'cat' | 'dog' | T[K];
};
type Test<A extends any[]> = MyType<A>;
`);
const node = sourceFile.statements.at(-1) as ts.TypeAliasDeclaration;
const type = typeChecker.getTypeAtLocation(node);

expect(
isPropertyReadonlyInType(
type,
ts.escapeLeadingUnderscores("length"),
typeChecker
)
).toBe(false);
});
});
9 changes: 5 additions & 4 deletions src/types/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ function isReadonlyPropertyFromMappedType(
!/^__@[^@]+$/.test(name as string)
)
return declaration.readonlyToken.kind !== ts.SyntaxKind.MinusToken;
return isPropertyReadonlyInType(
(type as unknown as { modifiersType: ts.Type }).modifiersType,
name,
typeChecker

const { modifiersType } = type as { modifiersType?: ts.Type };

return (
modifiersType && isPropertyReadonlyInType(modifiersType, name, typeChecker)
);
}

Expand Down

0 comments on commit 405c475

Please sign in to comment.