typescript/no-unsafe-class-instance-assignment Suspicious
What it does
Disallows using structurally compatible values where an instance of a class or one of its subclasses is expected. It also checks values nested in types such as arrays and promises.
Why is this bad?
TypeScript's structural typing can accept an object with the same public members as a class instance. The object can bypass initialization in the class constructor and fail an instanceof check.
Examples
Examples of incorrect code for this rule:
ts
class Session {
constructor(readonly id: string) {}
}
const session: Session = { id: "guest" };Examples of correct code for this rule:
ts
class Session {
constructor(readonly id: string) {}
}
const session: Session = new Session("guest");How to use
To enable this rule using the config file or in the CLI, you can use:
json
{
"options": {
"typeAware": true
},
"rules": {
"typescript/no-unsafe-class-instance-assignment": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
options: { typeAware: true },
rules: {
"typescript/no-unsafe-class-instance-assignment": "error",
},
});bash
oxlint --type-aware --deny typescript/no-unsafe-class-instance-assignmentVersion
This rule was added in vnext.
