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

TypeScript type error cannot assign unknown to string... #1422

Closed
5 tasks done
ghiscoding opened this issue Mar 7, 2024 · 6 comments · Fixed by #1427
Closed
5 tasks done

TypeScript type error cannot assign unknown to string... #1422

ghiscoding opened this issue Mar 7, 2024 · 6 comments · Fixed by #1427

Comments

@ghiscoding
Copy link
Owner

Describe the bug

I think the upgrade to the new TypeScript version created this new issue but only after I deleted the pnpm lock file and reinstalled everything

type Join<T extends any[], D extends string> =
T extends [] ? never :
T extends [infer F] ? F :
T extends [infer F, ...infer R] ?
F extends string ? string extends F ? string : `${F}${D}${Join<R, D>}` : never : string;

image

Reproduction

pnpm install and then run a TypeScript build

Which Framework are you using?

Vanilla / Plain JS

Environment Info

n/a

Validations

@ghiscoding
Copy link
Owner Author

ghiscoding commented Mar 7, 2024

@zewa666 can you help with this error since it is code you've put in couple years ago 😉

I don't know how to cast within a type, I think for now, I'll simply add a @ts-ignore. The unknown and never are still really confusing to me.

EDIT

if I replace never with any then it no longer throw, however I'm not sure if that will follow the original intent

type Join<T extends any[], D extends string> =
   T extends [] ? never :
   T extends [infer F] ? F :
   T extends [infer F, ...infer R] ?
-  F extends string ? string extends F ? string : `${F}${D}${Join<R, D>}` : never : string;
+  F extends string ? string extends F ? string : `${F}${D}${Join<R, D>}` : any : string;

I think I'll go with that since the first never on line 3 doesn't have to change, so the end result will probably still be never since the Join is recursive, the last one will be never (unless other returns are found)

@ghiscoding ghiscoding changed the title TypeScript type error cannot assign unknown to string... TypeScript type error cannot assign unknown to string... Mar 7, 2024
@zewa666
Copy link
Contributor

zewa666 commented Mar 7, 2024

hmm I'm not next to a Computer to be able to try it out but I'd assume that with the new TS version there is better recusrive inference, hence the issue could be the T of type any[].

perhaps if we replace T extends any[] with T extends (string | number | boolean | Date)[] it would make sure that there are no unknown cases.

I can check that one out tomorrow

EDIT:
the original intent of join is to make sure that whatever parts are provided have a string representation and are an array of at least 1 elem. so with only one item it becomes the item itself. if more elements of the array are provided it checks recursively every tail (all array elements minus head) for the same condition.

never makes sure that we do not get into a case were there is something thats not joinable eg T = symbol[]. if you switch that for any you'd essentially loose the type safety and the recursive inference.

@ghiscoding
Copy link
Owner Author

ghiscoding commented Mar 7, 2024

alright, I'll let you propose a fix then if you don't mind. Thanks 😉

ohh and BTW, I added Stackblitz on the main readme of the project, so you don't even need to clone the project anymore, you can use Stackblitz and use VSCode within Stackblitz, it's kinda cool since the project is all running within that container. It's also adding itself in every PR, so you could use the Stackblitz from last PR #1423 which is where I've added @ts-ignore for now

@zewa666 zewa666 mentioned this issue Mar 8, 2024
@zewa666
Copy link
Contributor

zewa666 commented Mar 8, 2024

that sounds really cool. So its a way to quickly review an incoming PR? very cool, i like.

btw I think I got the PR ready. Please try this one out locally though, since I can't reproduce the IDE error and I'm not sure Stackblitz would either.

@ghiscoding
Copy link
Owner Author

that sounds really cool. So its a way to quickly review an incoming PR? very cool, i like.

@zewa666
Yeah I just added Stackblitz app to my GitHub account and a Stackblitz link to my readme page (in this commit eaf3bfd) and that was it, I was actually surprised about the PR, I just wanted a Stackblitz button (I copied the link from a GitHub project I saw and just tried it for fun but I find it to be very useful). More info on Stackblitz add PR new button

BTW, have you had a chance to try the Dark Mode yet? 😉

@zewa666
Copy link
Contributor

zewa666 commented Mar 8, 2024

no, sadly I'm burried in work with AI stuff currently. But finally begin of April my larger grid project is about to begin so I'm looking forward getting up to speed with all the new features.

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

Successfully merging a pull request may close this issue.

2 participants