Giter VIP home page Giter VIP logo

Comments (13)

semlinker avatar semlinker commented on May 18, 2024 11
type TrimLeft<V extends string> = V extends ` ${infer R}` ? TrimLeft<R> : V;
type TrimRight<V extends string> = V extends `${infer R} ` ? TrimRight<R> : V;

type Trim<V extends string> = TrimLeft<TrimRight<V>>;

// 测试用例
Trim<' semlinker '>
//=> 'semlinker'

from awesome-typescript.

zhaoxiongfei avatar zhaoxiongfei commented on May 18, 2024 5
// 实现一个 Trim 工具类型,用于对字符串字面量类型进行去空格处理。具体的使用示例如下所示:
type TrimLeft<T extends string> = T extends ` ${infer A}` ? TrimLeft<A> : T;
type TrimRight<T extends string> = T extends `${infer A} ` ? TrimRight<A> : T;

type Trim<V extends string> = TrimLeft<TrimRight<V>>;

// 测试用例
type T1 = Trim<' semlinker '>
type T2 = Trim<' semlinker              '>
type T3 = Trim<'             semlinker              '>
//=> 'semlinker'

这题需要掌握的是 extends 配合 infer 在字符串中的使用,之后就是映射类型可以递归。
另外注意Trim分解成TrimLeft 和 TrimRight是分治的**。分而治之,有效的简化了问题

from awesome-typescript.

jeffacode avatar jeffacode commented on May 18, 2024 4
type Trim<V extends string> = V extends ` ${infer R}` | `${infer R} ` ? Trim<R> : V
type foo = Trim<'    semlinker    '>

from awesome-typescript.

yitjhy avatar yitjhy commented on May 18, 2024 2
type Trim<T extends string> = T extends `${infer F}${infer Rest}`
  ? F extends ' '
    ? Trim<Rest>
    : `${F}${Trim<Rest>}`
  : ''
type TTestTrim = Trim<' s em link    er '>
// semlinker

from awesome-typescript.

fishcoderman avatar fishcoderman commented on May 18, 2024 1
type Trim<V extends string> = V extends ` ${infer R} ` ? R : V; // 你的实现代码

// 测试用例
type R = Trim<' semlinker '>

from awesome-typescript.

Mrlgm avatar Mrlgm commented on May 18, 2024
type TrimLeft<V extends string> = V extends ` ${infer U}` ? TrimLeft<U> : V
type TrimRight<V extends string> = V extends `${infer U} ` ? TrimRight<U> : V

type Trim<V extends string> = TrimLeft<TrimRight<V>>

// 测试用例
type Result = Trim<' semlinker '>
//=> 'semlinker'

from awesome-typescript.

liulise avatar liulise commented on May 18, 2024

type TrimLeft<V extends string> = V extends ` ${infer C}` ? TrimLeft<C> : V;
type TrimRight<V extends string> = V extends `${infer C} ` ? TrimRight<C> : V;
type Trim<V extends string> = TrimLeft<TrimRight<V>>;

from awesome-typescript.

ShawDuChen avatar ShawDuChen commented on May 18, 2024
// 去除左侧空格
type TrimLeft<V extends string> = V extends ` ${infer R}` ? TrimLeft<R> : V
// 去除右侧空格
type TrimRight<V extends string> = V extends `${infer L} ` ? TrimRight<L> : V
type Trim<V extends string> = TrimRight<TrimLeft<V>>

from awesome-typescript.

smartymoon avatar smartymoon commented on May 18, 2024

好多题目都融合了巧妙的算法啊

from awesome-typescript.

waleiwalei avatar waleiwalei commented on May 18, 2024
// 测试用例
type R = Trim<' semlinker '>
//=> 'semlinker'

type Trim<V extends string, Str extends string = ''> = 
  V extends `${infer First}${infer Rest}`
    ? First extends ' '
      ? Trim<Rest, Str>
      : Trim<Rest, `${Str}${First}`>
    : Str

from awesome-typescript.

YJCCreateAHistory avatar YJCCreateAHistory commented on May 18, 2024
type TrimL<V extends string> = V extends `${infer P}` ? TrimL<P> : V
type TrimR<V extends string> =  V extends `${infer P}` ? TrimR<P> : V
type Trim<V extends string> = TrimL<TrimR<V>>// 你的实现代码

// 测试用例
Trim<' semlinker '>
//=> 'semlinker'

from awesome-typescript.

MuLoo avatar MuLoo commented on May 18, 2024

这题妙啊

from awesome-typescript.

SweeperDuan avatar SweeperDuan commented on May 18, 2024
type Trim<V extends string> = V extends ` ${infer S} ` ? Trim<S> : V

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.