-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Add missing toString
declarations for base types that have them
#38347
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
In general we don't like modifying the built-in definitions unless there's a good reason (it's quite dangerous), and frankly "This lint rule is relies on incorrect assumptions about TypeScript's type system" doesn't seem like a good reason. Why not just add exclusions for those three types you listed? |
I understand that there is a chance for false positives that may occur, and much slimmer chance for a false negative, but in userland when it reports an error, it's going to be on an object type. And likely the fix isn't going to be "add a OOC, why is it quite dangerous to modify them in an additive fashion? I can understand changes and deletions being dangerous, but shouldn't additions be mostly safe? We can add exceptions, it's just that the exceptions are somewhat cumbersome to build and maintain. A naive name-based exclusion won't work well, as it will also exclude user-defined types that shadow these builtins, so instead it means we have to do something like asserting that the declaration for the type exists in a library file - which is a large branch from the logic as it stands that doesn't get the type's declaration. cc @JoshuaKGoldberg - the original rule author. |
The danger is really in the unknown unknowns. We generally find unexpected and unanticipatable breaks for any change to widely-used built in types. |
In this case, the built-in declaration is wrong, why it shouldn't be fixed? |
Regarding the missing definition for the This would allow patterns like this (valid in JS) without having to resort to declaration merging in user code: type SorterMap<T> = Record<"true" | "false", (a: T, b: T) => number>;
const makeTdStrSorter = ({ up = true }) => <
T extends string
>(
a: T,
b: T
) => {
const dirMap: SorterMap<T> = {
true: (a, b) => (a < b ? -1 : a > b ? 1 : 0),
false: (a, b) => (a > b ? -1 : a < b ? 1 : 0),
};
return dirMap[up.toString()](a, b); //expression of type 'string' can't be used to index type 'SorterMap<T>'
}; I do understand and appreciate the concern that changes to the standard library may cause things to break, but this is about adding something that should've probably been there from the get-go, so if it does break something, it is better to know it now than further down the road. |
I added export {};
declare global {
interface Boolean {
toString(): string;
}
} I hope this problem will be fixed in the library side. 🥺 |
I've encountered the same problem with Boolean.toString() method and it was kinda frustrating to get an error for this method, especially for junior developer (I thought I've been doing something wrong). |
Fix lint errors.
Search Terms
toString, RegExp, Regular Expression, Boolean, Error
Suggestion
In @typescript-eslint, we have a rule
no-base-to-string
. This rule ensures that any value that's being coerced to a string has atoString
method defined on it.This is to help catch cases where you accidentally coerce something to a string, and end up with an
[object Object]
in your strings.This rule has a pretty simple implementation - it just gets the declarations of the
toString
method on the type, and ensures that none of the declarations belong to theObject
type.The logic relies upon the fact that every type that actually has a
toString
method explicitly declares atoString
method.A few of our users ran into cases that were reporting errors for them, even though they shouldn't (typescript-eslint/typescript-eslint#1655).
There's one that specifically has caused people some issues:
Boolean
/boolean
.I eyeballed the docs and the types, and I am pretty certain there are only 3 types that are missing a
toString
declaration:Related
PR: #37839
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: