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
Bug: [@typescript-eslint/no-unnecessary-type-assertion] False positives for "as const" assertions #8721
Comments
This seems like a bug in TS. This is evidenced by the fact that TS types your interface as requiring the property The fact that TS widens the object type to have It's probably worth filing an issue upstream. |
This comment was marked as off-topic.
This comment was marked as off-topic.
@klassm you can remove the first This issue is specifically about |
Hi @bradzacher, |
Maybe, but as it stands the rule is doing more harm than good by wrongly flagging such |
Hmmmm, note also that -export const marker = "marker" as const;
+export const marker = "marker";
export interface Data {
marker: typeof marker;
value: string;
}
function createData(): Data {
- const base = { marker: marker };
+ const base = { marker: marker } as const;
return { ...base, value: "" };
} works... and makes a whole lot more sense anyway. It seems really odd that using |
In this case, yes, but that will affect all other attributes of the object. For example, if |
-export const marker = "marker" as const;
+export const marker = "marker";
export interface Data {
marker: typeof marker;
value: string;
}
function createData(): Data {
- const base = { marker: marker };
+ const base: { marker: typeof marker } = { marker: marker };
return { ...base, value: "" };
} works also. As does -export const marker = "marker" as const;
+export const marker = "marker";
export interface Data {
marker: typeof marker;
value: string;
}
function createData(): Data {
- const base = { marker: marker };
+ const base = { marker: marker as typeof marker };
return { ...base, value: "" };
} and, of course -export const marker = "marker" as const;
+export const marker = "marker";
export interface Data {
marker: typeof marker;
value: string;
}
function createData(): Data {
const base = { marker: marker };
- return { ...base, value: "" };
+ return { marker, value: "" };
} So it has something to do specifically with the widening behavior when creating a mutable object whose type is inferred. More I think about it, it really just seems surprising to me personally that the example in the issue report works in the first place. |
@kgregory yeah, that's a fair point. and that irked me too. See #8721 (comment) for 3 more examples that don't though 🤣 |
I also note that putting I guess it seems like we're running into a case where a type assertion has broader effect than setting the type of the thing being asserted on.
Curious what other team members think |
I've just learned that TS appears to have intentional behavior around the "freshness" of a In any case, I think it explains what we're seeing here, though I'm not sure that it is obvious what we should actually do about it. @bradzacher, thoughts? |
Before You File a Bug Report Please Confirm You Have Done The Following...
Playground Link
https://typescript-eslint.io/play/#ts=5.4.2&fileType=.tsx&code=KYDwDg9gTgLgBAYwgOwM7wLYEMoGthRwC8cARNngaXFqoiugNwCwAUKJLHAJbIwEAzLAmBwAIlhhY4AbzZw4FfFABccGAE8wwCAMU5lLVgoBuWADYBXYGvRReAcyMBfNgMvIEMbikRRgksASUgAUAJRqwdJyxvRo8ABGtKIkMvqUqunKcM5GCv4wllDIsnAAdBVJqMAANHBmVjZk1LlszkA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWkOTJE0fJQ5N0UOdA7RI4MAF8QOoA&tsconfig=N4KAvkA&tokens=false
Repro Code
ESLint Config
tsconfig
Expected Result
The first line of this code should not report an error for the
as const
assertion, since this assertion is necessary - removing it is a TypeScript compilation error (see TypeScript playground).Actual Result
Starting in typescript-eslint@7.3 (presumably #8558), the first line of this code reports a @typescript-eslint/no-unnecessary-type-assertion error.
Additional Info
No response
The text was updated successfully, but these errors were encountered: