Skip to content

Uncapitalize seems to not working within a type helper #52102

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

Open
bedis-elacheche opened this issue Jan 4, 2023 · 2 comments · May be fixed by #52112
Open

Uncapitalize seems to not working within a type helper #52102

bedis-elacheche opened this issue Jan 4, 2023 · 2 comments · May be fixed by #52112
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@bedis-elacheche
Copy link

Bug Report

🔎 Search Terms

Uncapitalize

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Uncapitalize

⏯ Playground Link

Playground link with relevant code

💻 Code

type CamelCase1<
  S extends string,
  L extends string = Lowercase<S>,
  Res extends string = ""
> = L extends ""
  ? Uncapitalize<Res>
  : L extends `${infer H}_${infer T}`
  ? CamelCase1<never, T, `${Res}${Capitalize<H>}`>
  : CamelCase1<never, "", `${Res}${Capitalize<L>}`>;

type test1 = CamelCase2<"FOOBAR">
//   ^? type test1 = "Foobar"

type CamelCase2<
  S extends string,
  L extends string = Lowercase<S>,
  Res extends string = ""
> = L extends ""
  ? Res
  : L extends `${infer H}_${infer T}`
  ? CamelCase2<never, T, `${Res}${Capitalize<H>}`>
  : CamelCase2<never, "", `${Res}${Capitalize<L>}`>;

type test2 = Uncapitalize<CamelCase2<"FOOBAR">>
//   ^? type test2 = "foobar"

🙁 Actual behavior

First letter is uppercase

🙂 Expected behavior

First letter should be lowercase

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Jan 4, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 4, 2023
@RyanCavanaugh
Copy link
Member

Something weird is happening. Here's a more obviously-wrong computation:

type CamelCase1<S extends string, L extends string = Lowercase<S>, Res extends string = ""> =
  L extends "" ? `start_${Uncapitalize<Res>}_end` : CamelCase1<never, "", `${Res}${Capitalize<L>}`>;

type test1 = CamelCase1<"ABC">
//   ^?
// start_Abc_end

@Andarist
Copy link
Contributor

Andarist commented Jan 4, 2023

When we hover over @RyanCavanaugh's CamelCase1 then we can see this definition:

type CamelCase1<
  S extends string,
  L extends string = Lowercase<S>,
  Res extends string = ""
> = L extends ""
  ? `start_${Uncapitalize<Res>}_end`
  : `start_${Uncapitalize<Res>}${Capitalize<L>}_end`;

As we can see - the falseType here looks quite different from the original. It has been simplified~ (I'm not sure if you call this process that).

The likely problem is that this conditional type is not fully deferred~. And if we consider this simplification, the observed result matches its "definition".

A similar thing happens with the original CamelCase1 by @bedis-elacheche

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
3 participants