Skip to content
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

Need some help to understand string extends S #183

Closed
PaGury opened this issue Feb 26, 2021 · 4 comments
Closed

Need some help to understand string extends S #183

PaGury opened this issue Feb 26, 2021 · 4 comments

Comments

@PaGury
Copy link

PaGury commented Feb 26, 2021

Hi !

First of all, thanks a lot for this library, it opened my eyes to the capacities and power of "String literal types".

I'm currently building a bunch of types to enable SQL query type extracting.
The main goal of my implementation will be to enable something like this :

const query = `
SELECT u.email as u_email, p.role as p_role
FROM users as u
INNER JOIN profiles as p on p.id = u.id`;
type ResultQueryType = GetQueryResultType<typeof query>;
// ResultQueryType = { u_email: any; p_role: any; }`

Actually, I have a working prototype deeply inspired by type-fest, but I ask myself a question and I didn't find anything concrete about the use of the condition string extends S in some of the library types (Here).
If I remove the condition string extends S the type is still working as expected.

Can someone explain to me the intention behind this ?

Thank you!

@papb
Copy link
Contributor

papb commented Feb 26, 2021

string extends S is a good way to check if the type S is exactly string, since by definition S extends string is already true.

(note: sometimes A extends B and B extends A can be both true at the sime time while still A and B not being the same, but for string this is valid)

@PaGury
Copy link
Author

PaGury commented Mar 1, 2021

Thanks,

I see, but I still don't understand why it's necessary to check this kind of assertion is the utility type Split.

export type Split<S extends string, D extends string> =
	string extends S ? string[] :
	S extends '' ? [] :
	S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] :
	[S];

In fact, if I remove the condition string extends S and if I pass an exact string as S, I still want to "Split" the string if it match the last condition ${infer T}${D}${infer U}. No ?
Why would we want to return a string[] instead of [S] in the case where S is a simple string ?

Sorry if I'm not clear, but it's kind of bug me 🤪 ! haha

@papb
Copy link
Contributor

papb commented Mar 1, 2021

Why would we want to return a string[] instead of [S] in the case where S is a simple string ?

Because if all you know about a value is that it is string, you have zero information about how many times whatever delimiter appears on it, so you cannot say anything more specific about the result other than string[]. If the result was [string] as you seem to suggest then you would be essentially saying something like

if you give me a variable of type string and ask me to split it with a delimiter 'foobar' then I am sure the result will be an array of length 1 (which is what [string] says)

Which is incorrect.

@PaGury
Copy link
Author

PaGury commented Mar 5, 2021

Ok I see !

Thanks a lot for your clear answers !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants