mylesmmurphy / prettify-ts

Prettify TypeScript: Better Type Previews
https://marketplace.visualstudio.com/items?itemName=MylesMurphy.prettify-ts
MIT License
264 stars 8 forks source link

Unexpected expansion of primitive types and built-in objects when used in a generic type parameter extends clause #47

Open kieselai opened 1 month ago

kieselai commented 1 month ago

All of the values in these types are expanded to include the full prototype of the built in primitive or object, specifically when used in an extends clause of a generic type parameter.

export interface ExtendingPrimitives<
  TString extends string,
  TNumber extends number,
  TBigInt extends bigint,
  TSymbol extends symbol,
  TBoolean extends boolean,
>
{
  string_val: TString,
  number_val: TNumber,
  bigint_val: TBigInt,
  symbol_val: TSymbol,
  boolean_val: TBoolean
}

image

export interface ExtendingBuiltInObjects<
  TRegex extends RegExp,
  TDate extends Date,
  TSet extends Set<any>
>
{
  regex_val: TRegex,
  date_val: TDate
  set_val: TSet
}

image

mylesmmurphy commented 2 weeks ago

Hi @kieselai Sorry for the delay again! This one is tricky... As far as I know, the TS Language Server doesn't provide a way to know what properties on a type are inherited from an object's prototype, but I could be wrong. I will look into this and get back to you. The TS LS does provide flags and checks for if a type is a built in primitive, which is how the current check prevents displaying all these ugly types prototype props. I will look into this and get back to you!

kieselai commented 4 days ago

Alternatively, if there is some way to check the type in the left hand side of an "extends" clause, maybe the expansion could be prevented based on a hard coded list of standard built-in types? The types listed out in Mozilla's docs could be useful for such an implementation.