-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Unsafe assignment of Record into interface with optionals #40529
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
Comments
The problem is much worse than you're suggesting 😉 interface OhNo {
prop?: string;
}
const a: Record<string, boolean> = { prop: true };
const b: OhNo = a;
b.prop; // claimed to be string | undefined, but is boolean In general index signatures are assumed to not contain every arbitrary key for the purposes of assignability, so If you want a feature request for this, it's probably #12936 |
Makes sense. On the other hand, I've been using Also, would you agree that the lint rule I proposed (assigning index signatures to interfaces with all optionals) makes sense then? To make developers aware of the type unsoundness that it may introduce.
You mean a feature request that would fix index signatures to behave more safely? I read the Exact Types feature request but I don't see the immediate connection. |
I try not to comment on lint settings; people have opinions and that's OK. That said, the "don't use |
Search Terms:
typescript unsafe Record unknown assignment interfaces all optional undefined sound type system
Code
I discovered the following code to be legal. It was pretty scary.
This is so dangerous for me* that I decided to propose a new eslint rule warning about it, but @bradzacher suggested I brought this issue to the TypeScript team. He also added the following example, which is even more intense because
unknown
isn't really relevant here:Expected behavior:
Compilation error.
Actual behavior:
Compilation successful.
*(can explain the use-case that led to the discovery if it's relevant)
I understand this might be an inevitable design limitation coming from index signatures, so this might be closer to a feature request than a bug? If this is the case, sorry for the wrong labeling.
The text was updated successfully, but these errors were encountered: