Giter VIP home page Giter VIP logo

Comments (9)

dolphin0618 avatar dolphin0618 commented on May 18, 2024 2
type RepeatString<
  T extends string,
  C extends number,
  A extends any[] = []
> = A['length'] extends C ? '' : `${T}${RepeatString<T, C, [...A, T]>}`

type S0 = RepeatString<"a", 0>; // ''
type S1 = RepeatString<"a", 2>; // 'aa'
type S2 = RepeatString<"ab", 3>; // 'ababab'

from awesome-typescript.

zhaoxiongfei avatar zhaoxiongfei commented on May 18, 2024 1
type RepeatString<T extends string, C extends number, A extends any[] = [], R extends string = ""> = A["length"] extends C
  ? R
  : RepeatString<T, C, [...A, ''], `${R}${T}`;

type S0 = RepeatString<"a", 0>; // ''
type S1 = RepeatString<"a", 2>; // 'aa'
type S2 = RepeatString<"ab", 3>; // 'ababab'

思路: 数字的比较只能利用 数组的 lenght属性来实现,字符串的 length属性在tsc阶段无法获取到真实长度,只能得到number类型。

from awesome-typescript.

zhaoxiongfei avatar zhaoxiongfei commented on May 18, 2024
type Repeat<T, C extends number, A extends any[] = []> = A["length"] extends C
  ? A
  : Repeat<T, C, [...A, T]>;

type Join<T extends string[], splitor extends string = ""> = T extends [infer F, ...infer R]
  ? R extends string[]
    ? F extends string
      ? `${F}${splitor}${Join<R, splitor>}`
      : ""
    : F
  : "";

type RepeatString<T extends string, C extends number> = Join<Repeat<T, C>, "">;

type S0 = RepeatString<"a", 0>; // ''
type S1 = RepeatString<"a", 2>; // 'aa'
type S2 = RepeatString<"ab", 3>; // 'ababab'

from awesome-typescript.

douhao1988 avatar douhao1988 commented on May 18, 2024
type Push<T extends any[], V> =  [...T, V];

type RepeatString<
  T extends string,
  C extends number,
  R extends string = '',
  A extends any[] = [],
> = A['length'] extends C ? R: (RepeatString<T, C, `${R}${T}`, Push<A, T>>)

type S01 = RepeatString<"a", 0>; // ''
type S11 = RepeatString<"a", 2>; // 'aa'
type S21 = RepeatString<"ab", 3>; // 'ababab'
type S22 = RepeatString<"abc", 3>; // 'abcabcabc'

from awesome-typescript.

xiaoYuanDun avatar xiaoYuanDun commented on May 18, 2024

用模板字符串进行递归就行了

type RepeatString<
  T extends string,
  C extends number,
  S extends any[] = [],     //  用于判断是否递归完毕
  U extends string = ''    //  用于累加记录已遍历过的字符串
> = S['length'] extends C ? U : RepeatString<T, C, [...S, 1], `${U}${T}`>

from awesome-typescript.

sunboyZgz avatar sunboyZgz commented on May 18, 2024

直接使用第九题的JoinStrArray操作Repeat后的元组

type RepeatString<T extends string, C extends number> = JoinStrArray<
  Repeat<T, C>,
  ""
>;

type S02 = RepeatString<"a", 0>; // ''
type S12 = RepeatString<"a", 2>; // 'aa'
type S22 = RepeatString<"ab", 3>; // 'ababab'

from awesome-typescript.

zjxxxxxxxxx avatar zjxxxxxxxxx commented on May 18, 2024
type _RepeatString<
  T extends string,
  C extends number,
  S extends string,
  Count extends string[]
> = Count["length"] extends C
  ? S
  : _RepeatString<T, C, `${S}${T}`, [...Count, T]>;

type RepeatString<T extends string, C extends number> = _RepeatString<
  T,
  C,
  "",
  []
>;

from awesome-typescript.

ChangerHe avatar ChangerHe commented on May 18, 2024
// 将RepeatString拆分成两步: 1. 将其转变成目标数组   2. 将数组拼接成为字符串
// 1. 通过RepeatStr将字符串和数量转变成为数组
// 如: <'ab', 2> => ['ab', 'ab']
type RepeatStr<S extends string, N extends number, R extends string[] = []> = R['length'] extends N ? R : RepeatStr<S, N, [...R, S]>
// 2. 创建一个Join帮助类型, 将重复的数组变成字符串
// 如: ['ab', 'ab'] => 'abab'
type Join<T extends string[]> = T extends [f: infer F, ...r: infer R] ? R extends string[] ? F extends string ? `${F}${Join<R>}` : '' : '' : ''

// 3. 组合上面的两个类型即可
type RepeatString<
  T extends string,
  C extends number,
> = Join<RepeatStr<T, C>>

from awesome-typescript.

liziqiang avatar liziqiang commented on May 18, 2024
type RepeatString<T extends string, C extends number, S extends string = '', R extends T[] = []> = R['length'] extends C
    ? S
    : RepeatString<T, C, `${S}${T}`, [...R, T]>; // 你的实现代码

type S0 = RepeatString<'a', 0>; // ''
type S1 = RepeatString<'a', 2>; // 'aa'
type S2 = RepeatString<'ab', 3>; // 'ababab'

from awesome-typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.