Skip to content

make Strings.startsWith to support ${Prefix}${string} #54971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zhujinxuan opened this issue Jul 11, 2023 · 3 comments
Closed

make Strings.startsWith to support ${Prefix}${string} #54971

zhujinxuan opened this issue Jul 11, 2023 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@zhujinxuan
Copy link

lib Update Request

Sometimes, we might make a more specific definition on string type to avoid confusion in programming. For example:

const statePrefix = `State/` as const
type State = `${typeof statePrefix}${string}`
const customerIdPrefix = `Customer/` as const
type CustomerId = `${typeof customerIdPrefix}`

In this way, we can distinguish state and customerId, rather than using string everywhere. For example, we can clearly know a structure of a dictionary like

// Avoid confusion of Map<string, Map<string, boolean>>
const IsActiveCustomerMap = new Map<State<Map<CustomerId, boolean>>>()

So, I suggest adding startsWith definition to understand literal types. I would suggest this definition added to the library in addition to the original definition:

    startsWith<T extends string>(searchString: T, position?: 0): this is `${T}${string};

This definition can help us simplify the specific string definition.

Configuration Check

My compilation target is ES2015 and my lib is the default.

Missing / Incorrect Definition

Add to Strings:

    startsWith<T extends string>(searchString: T, position?: 0): this is `${T}${string};

Sample Code

function mkPrefix<T extends string>(prefix: T, key: string): `${T}${string}` {
  if (key.startsWith(prefix)) return key;
  return `${prefix}${key}`
}

Documentation Link

@zhujinxuan
Copy link
Author

I can make the PR if this change is acceptable.

@zhujinxuan zhujinxuan changed the title Add specific definition to suppert ${Prefix}${string} make Strings.startsWith to support ${Prefix}${string} Jul 11, 2023
@MartinJohns
Copy link
Contributor

Sounds like a duplicate of #46958.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 11, 2023
@zhujinxuan
Copy link
Author

Yes, it is a duplicate. I will close the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants