question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

string module - smart 'template literal' return types

See original GitHub issue

🚀 Feature request

Use string ‘template literal’ behavior for ‘smarter’ return types

Current Behavior

import { pipe } from 'fp-ts/function'
import { startsWith } from 'fp-ts/string'
import * as O from 'fp-ts/Option'

declare const el: '00' | '01' | '10'

const a = pipe(
  el, 
  O.fromPredicate(startsWith('1')),
)
// a: Option<'00' | '01' | '10'>

Desired Behavior

// a: Option<'10'>

Suggested Solution

const startsWith = <S extends string, E extends string>(e: E) => (s: S): 
  s is S extends `${E}${infer _}` ? S : never => s.startsWith(e)

Similar improvements could be made to toUpperCase, toLowerCase, trim, trimLeft, trimRight, isEmpty, surround, unsurround, dropLeft, dropRight, head, tail, and init

size and split, lookup, replaceAll, lines and unlines can also be implemented, but they’re a bit more complicated (links from ts-toolbelt)

Who does this impact? Who is this for?

Users manipulating string literal types

Describe alternatives you’ve considered

Could be put into a separate module (maybe fp-ts-std? @samhh)

Additional context

It’s a bit tricky to decide how complicated these types can get - many of these can be implemented simply, but there are a few that are more complicated.

We could import ‘ts-toolbelt’, but I’m sure that would have package size implications, and it might not be worth the effort to maintain anything more than the simplest type definitions

Your environment

Software Version(s)
fp-ts 2.12
TypeScript 4.6

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
thewilkybarkidcommented, Mar 17, 2022

I just meant whether to have a utility type like StartsWith or a more complex type predicate. To me, Json is a ‘regular’ type whereas StartsWith is a ‘utility’ type (as StartsWith<'one', 'two'> is never).

Either way, it’s definitely a refinement. 😃

0reactions
anthonyjoesephcommented, Mar 21, 2022

or a more complex type predicate

Do you mean something like this?

export const startsWith: {
  <Start extends string>(
    s: Start
  ): <Full extends string>(
    f: Full
  ) => f is string extends Full
    ? string extends Start 
      ? string 
      : Extract<`${Start}${string}`, string> 
    : Extract<Full, `${Start}${string}`>
  (e: string, n?: number): (s: string) => boolean
} = <S2 extends string>(e: string, n?: number) => 
  <S extends string>(s: S): s is StartsWith<S, S2> => s.startsWith(e, n)
// no exported `StartsWith` utility type

imo that’s a bit harder to read. Is there a disadvantage to exporting the StartsWith type?

Here’s what I’m thinking for a complete implementation of the new functions - how does that look?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Template literals (Template strings) - JavaScript | MDN
Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, string interpolation with embedded ...
Read more >
Documentation - Template Literal Types - TypeScript
Template literal types build on string literal types, and have the ability to expand into many strings via unions. They have the same...
Read more >
Exploring Template Literal Types in TypeScript 4.1
Using template literal types we can reimplement our case conversion at the type level. This requires another handy new feature - four new...
Read more >
I need to learn about TypeScript Template Literal Types
TypeScript has a string type. It covers all strings like const hello = "Hello World"; , or const myName = `My name is...
Read more >
TypeScript Splits the Atom!
Template literal types This is the key advance. Template literal types allow you to find structure inside string literal types and create ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found