Is there a way to prevent the compiler from overloading toString? I've tried using the never type, but it still allows implicit assignments and only raises an error when something is done with the variable. It's inconvenient to remember to explicitly declare toString calls as strings.
type ArrayToStringMethod = {
(this: { join(a: string): string, length: number }): string
(this: any): never // if this overload isn't here typescript uses
// Object.toString automatically.
}
type ArrayDontMutate<t> = {
toString: ArrayToStringMethod
readonly [index: number]: t
readonly length: number
} &
Pick<
Array<t>,
'find' |
'map' |
//'join' | deliberately removed to make toString fail
'some' |
'slice' |
'concat' |
'reduce'>
let a:ArrayDontMutate<string> =['a','b','c'] as any
let b = a.toString() //should fail no join method. b is the never type