ts-reset icon indicating copy to clipboard operation
ts-reset copied to clipboard

String methods properly infering types

Open KajSzy opened this issue 2 years ago • 1 comments

Methods such as toLowerCase and toUpperCase do not properly infer union type that are invoked on.

Typescript Playground Link

There is solution for that, but due to those methods being very ground level it may break things in legacy code. Thus official typescript is not so keen to change those types.

interface String {
  toLowerCase<T extends string>(this: T): Lowercase<T>;
  toUpperCase<T extends string>(this: T): Uppercase<T>;
}

KajSzy avatar Feb 20 '23 18:02 KajSzy

Folks, if you want to go nuts with strongly-typed string functions, check out string-ts library =)

gustavoguichard avatar Oct 07 '23 23:10 gustavoguichard

I'm coming back to this issues list a year later and I want to define some reasonable rules for what ts-reset should and shouldn't accept. I think string methods inferring proper types is not part of that.

My thinking is that tight string inference should be something that you opt-in to at the call site. @gustavoguichard's library string-ts is a great example of this.

My reasoning is that you don't care about this behavior MUCH more often than you do. Most of the time, you just want to deal in string inputs and string outputs. Adding this extra overhead to the TS compiler doesn't seem right to me.

Plus - I want to keep ts-reset small. In that spirit, I won't be adding this to the library.

Thanks for the issue!

mattpocock avatar May 27 '24 15:05 mattpocock