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

fix: account for mapped types without modifiers in isPropertyReadonlyInType #18

Merged
merged 2 commits into from
Feb 15, 2023
Merged
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
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 };
Copy link
Collaborator

Choose a reason for hiding this comment

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

We might want to create a hasModifiersType utility function.
Lets do this in another PR though.


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

Expand Down