02688-medium-startswith

Back

// your answers
type StartsWith<T extends string, U extends string> = T extends `${U}${infer R}` ? true : false

Solution by AndreGeng #34759

Imagine that ${U}${string} is simply not exist or you don't know about it. How could you solve this problem and invent your own bicycle? Please don't do this in real life

type StartsWith<T extends string, U extends string> = 
  ZeroLength<U> extends true
  ? true
  : LengthOfOne<U> extends true 
    ? FirstSymbol<U> extends FirstSymbol<T>
      ? true
      : false
    : StartsWith<Teil<T>, Teil<U>>  

type ZeroLength<T extends string> = T extends '' ? true : false
type LengthOfOne<T extends string> = T extends `${infer _}${infer B}` ? ZeroLength<B> : false
type FirstSymbol<T extends string> = T extends `${infer U}${infer _rest}` ? U : never
type Teil<A extends string> = A extends `${infer _}${infer Teil}` ? Teil : never

Here is the algorithm:

  1. Check if the U string is zero-length, exist or equal ''. If so, return true
  2. Check if the U string length equals one, then all you need to do is to compare U string to first symbol of T string and return result
  3. If the U string length more than 1, check if the first U symbol equals to the first symbol T string. If not - T string is not starts with U string. If yes - do all this recursively with the rest of the both strings

Solution by alex-altay #34695

type StartsWith<T extends string, U extends string> = T extends `${U}${infer R}` ? true : false

Solution by ouzexi #34047

2688 - StartsWith

by jiangshan (@jiangshanmeta) #medium #template-literal

Question

Implement StartsWith<T, U> which takes two exact string types and returns whether T starts with U

For example

type a = StartsWith<'abc', 'ac'> // expected to be false
type b = StartsWith<'abc', 'ab'> // expected to be true
type c = StartsWith<'abc', 'abcd'> // expected to be false

View on GitHub: https://tsch.js.org/2688

...

type StartsWith<T extends string, U extends string, S0 extends string = ''> =
  S0 extends U
  ? true
  : T extends `${infer L}${infer R}`
    ? StartsWith<R, U, `${S0}${L}`>
    : false


type StartsWith<T extends string, U extends string> = T extends `${U}${string}`?true:false

Solution by veralex #33806

type StartsWith<T extends string, U extends string> = T extends `${U}${infer Last}` ? true : false;

Solution by ZhipengYang0605 #33110

// your answers
type StartsWith<T extends string, U extends string> = T extends `${U}${infer R}` ? true : false

Solution by pea-sys #32851

// 你的答案
type StartsWith<T extends string, U extends string> = T extends `${U}${string}` ? true:false

Solution by shanguoteng #32815

type StartsWith<S1 extends string, S2 extends string> = S1 extends `${S2}${infer Rest}` ? true : false

type a = StartsWith<'abc', 'ac'> // expected to be false
type b = StartsWith<'abc', 'ab'> // expected to be true
type c = StartsWith<'abc', 'abcd'> // expected to be false

Solution by ZhulinskiiDanil #32712

type StartsWith<T extends string, U extends string> = T extends `${U}${infer _}` ? true : false

Solution by dev-hobin #32493

type StartsWith<T extends string, U extends string> = T extends `${U}${string}`? true:false

Solution by rkamely #32065

type StartsWith<T extends string, U extends string> = T extends `${U}${infer R}` ? true : false;

Solution by ricky-fn #31872

T extends `${U}${infer P}` ? true:false

Solution by Zhen-code #31801

type StartsWith<T extends string, U extends string> = T extends `${U}${infer T}`
  ? true
  : false;

Solution by vipulpathak113 #31632

// your answers
type last = string
type StartsWith<T extends string, U extends string> = T extends `${U}${last}`? true:false;

Solution by kangaroona #31149

// your answers
I think it's a little bit overdone

type IsMore<A, B> = A extends `${infer AF}${infer AR}` ? B extends `${infer BF}${infer BR}` ? IsMore<AR, BR> : true : false;

type _StartWith<T extends string, U extends String> =
  T extends `${infer TF}${infer TR}` ? U extends `${infer UF}${infer UR}` ?
  TF extends UF ? _StartWith<TR, UR> : false : true : true;

type StartsWith<T extends string, U extends string> = IsMore<U, T> extends true ? false : _StartWith<T, U>;

Solution by chenqy-yh #31080

type StartsWith<T extends string, U extends string> = 
  T extends `${infer F}${infer Rest}`? 
  U extends `${infer UF}${infer URest}` ?
  F extends UF ?
  StartsWith<Rest, URest> :
  false :
  true :
  U extends '' ? 
  true :
  false

Solution by gearonixx #30881

type StartsWith<T extends string, U extends string> = T extends `${U}${string}` ? true : false;

Solution by kai-phan #30836

type StartsWith<T extends string, U extends string> = T extends `${U}${infer Rest}` ? true : false

Solution by maximallain #30555

type StartsWith<T extends string, U extends string> = T extends `${U}${string}` ? true : false;

Solution by kanishev #29854

复用已有的ReplaceAll

type StartWith<T extends string, E extends string> = T extends `${E}${infer R}` ? true : false;
type a1 = StartWith<"abcd", "abcd">;

Solution by sundial-dreams #29482

Solution:

type StartsWith<T extends string, U extends string> = T extends `${U}${string}` ? true : false;

Solution by DmitriiBr #29353

Cumbersome but, it is works
type StartsWith<T extends string, U extends string> = T extends `${infer L}${infer R}`
  ? U extends `${infer Lm}${infer Rm}`
    ? [Lm] extends [never]
      ? true
      : Lm extends L
      ? StartsWith<R, Rm>
      : false
    : true
  : T extends U
  ? true
  : false;

Solution by dmytro-shumak #29255

type StartsWith<T extends string, U extends string> = T extends ${U}${string} ? true : false;

Solution by DoubleWoodLin #28690

type First<T extends string> =  T extends `${infer F}${infer R}` ?  F : never;
type Rest<T extends string> = T extends `${infer F}${infer R}` ?  R : never;
type StartsWith<T extends string, U extends string> = U extends '' 
  ? true 
  : First<T> extends First<U> 
    ? First<T> extends never 
      ? false 
      : StartsWith<Rest<T>, Rest<U>> 
    :false

Solution by breakinferno #28458

// your answers
type StartsWith<T extends string, U extends string> = T extends `${U}${infer R}` ? true : false;

Solution by GreattitJY #27657

type StartsWith<T extends string, U extends string> = T extends `${U}${string}` ? true : false

Solution by jjswifty #27624

type StartsWith<T extends string, U extends string> = T extends `${U}${string}` ? true : false;

Solution by jjswifty #27385

// your answers
type StartsWith<T extends string, U extends string> = T extends `${U}${string}` ? true : false

Solution by daiki-skm #27305

type StartsWith<T extends string, U extends string> = T extends `${U}${infer O}` ? 
	true : false

Solution by 8471919 #27207

type StartsWith<T extends string, U extends string> = T extends `${U}${string}` ? true : false;

Solution by RainbowIsPerfect #26998