[Typescript] 115. Hard - Drop String
typescript string drop Hard 115
2023-09-14 09:00:44 时间
Drop the specified chars from a string.
For example:
type Butterfly = DropString<'foobar!', 'fb'> // 'ooar!'
/* _____________ Your Code Here _____________ */
type StringToUnion<S> = S extends `${infer F}${infer RT}` ? F | StringToUnion<RT>: never;
type DropString<S, R> = R extends '' ? S : S extends `${infer F}${infer RT}`
? F extends StringToUnion<R> ? DropString<RT, R>: `${F}${ DropString<RT, R>}`
: S;
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<DropString<'butter fly!', ''>, 'butter fly!'>>,
Expect<Equal<DropString<'butter fly!', ' '>, 'butterfly!'>>,
Expect<Equal<DropString<'butter fly!', 'but'>, 'er fly!'>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', 'but'>, ' e r f l y ! '>>,
Expect<Equal<DropString<' butter fly! ', ' '>, 'butterfly!'>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', ' '>, 'butterfly!'>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', 'but'>, ' e r f l y ! '>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', 'tub'>, ' e r f l y ! '>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', 'b'>, ' u t t e r f l y ! '>>,
Expect<Equal<DropString<' b u t t e r f l y ! ', 't'>, ' b u e r f l y ! '>>,
]
相关文章
- [Typescript] Use never for readable string
- [Typescript] DOMNodeExtractorConfig
- [Typescript] 115. Hard - Drop String
- [Typescript] 102. Hard - String to Number
- [Typescript] Declaration Merging
- [Typescript] Tuple type usage example & Discriminated Unions
- [TypeScript] “typeof” Type Queries
- [TypeScript] Collect Related Strings in a String Enum in TypeScript
- [TypeScript] Increase TypeScript's type safety with noImplicitAny
- [TypeScript] Generating Definition Files
- [Typescript] Generics in a Class Names Creator
- [Typescript] Key in union type
- [Typescript] Create a Union of Strings with All Possible Permutations of Two Unions
- [Typescript] Variable assignment - extends infer X
- [Typescript] Tips: Decode URL search params at the type level with ts-toolbelt
- [Typescript] Making TypeScript Stick - 5 - Extends
- [Typescript] Discriminated (Tagged) Unions
- [Typescript] User defined type guards
- [Typescript] “Record” Mapped Type
- [TypeScript] Increase TypeScript's type safety with noImplicitAny
- typescript入门简明教程
- TypeScript class 构造函数和成员的初始化顺序