Just found an issue with the code. We need to use „false“ instead of „never“ in the „CorrectStart“ type because never extends all types. Thanks for pointing that out. Also we need to add the "(" in the AllowedStarts. Checkout my github repository for the newest code and feel free to send a pull request when you find other errors: github.com/typed-rocks/typescript/blob/main/calculator.ts The
This won't accept unary prefix operators (such as -, as in -1), which is fine. However, what's not fine is that it will also accept such expression: 3 3 + 2
The parentheses check is overengineered. I don't think you should have the stack of parentheses, because in fact the only value you store is "(". And what you are actually checking is if the amount of opening parentheses equals to amount of closing. What you could do instead, is to recursively extract "()" from parentheses string while possible and then check whether the leftover is empty or not.
Mindblowing stuff again :) I noticed an issue: although operators weren't set as allowed starts (although minus should be as a sign of a number) the expression '/5+3' is valid but it clewarly shouldn't be the case. My solution was this: type CorrectStart = T extends ... ? First ... ? true : never : false // was 'never' before The last word was the only change. I don't know why this works but it does
@@tngerik14 Yes, absolutely, I've updated the code on github. We need to also allow a "(" at the values of AllowedStarts: github.com/typed-rocks/typescript/blob/main/calculator.ts
You basically have a mini compiler frontend. Now you gotta make it actually compute... Or make a piece of code that generates this based on a generic language grammar. This way, if you manage to get the typescript syntax working, you can run typescript from a string, at compile time... Thinking about this a bit more, if you add some sort of interface to that ts interpreter, you could produce some actual types at compile time as output of your inline ts code. But wait, what if you run this ts compile inside of itself...
Seeing all this magic i really don't understand this moaning of libraries authors that they have a problem with TypeScript and types... clearly just skill issue 🤣
Just found an issue with the code. We need to use „false“ instead of „never“ in the „CorrectStart“ type because never extends all types. Thanks for pointing that out. Also we need to add the "(" in the AllowedStarts. Checkout my github repository for the newest code and feel free to send a pull request when you find other errors: github.com/typed-rocks/typescript/blob/main/calculator.ts
The
finally, someone did a good programming video that is not useless or clickbait.
Awesome work 🎉
Thank you. I try my best to fulfill whats on the thumbnail 😁
This is actually peak, great job dude!
Thank you, appreciate it 😁
I'm impressed. Next crazy thing is to create a type to validate a whole TypeScript syntax. Let's make a full circle!
That would be next level 😁
I feel like I've just leveled up my TypeScript skills. You're a great teacher. Thanks!
Many thanks. That is really kind of you. Glad you like my presentation style :)
That was nuts! Extremely unusable, but extremely enjoyable! Let's follow it by something real life usable!:)
I will. Thanks 🙏
It's so difficult, but so powerful, great!
This won't accept unary prefix operators (such as -, as in -1), which is fine.
However, what's not fine is that it will also accept such expression: 3 3 + 2
Right, we would need to check for spaces between numbers. I will update my github later today
"Hope you learned something useful today". Not sure about the useful bit, but otherwise I did learn something 🤣
The parentheses check is overengineered. I don't think you should have the stack of parentheses, because in fact the only value you store is "(". And what you are actually checking is if the amount of opening parentheses equals to amount of closing.
What you could do instead, is to recursively extract "()" from parentheses string while possible and then check whether the leftover is empty or not.
Mindblowing stuff again :)
I noticed an issue: although operators weren't set as allowed starts (although minus should be as a sign of a number) the expression '/5+3' is valid but it clewarly shouldn't be the case.
My solution was this:
type CorrectStart =
T extends ...
? First ...
? true
: never
: false // was 'never' before
The last word was the only change. I don't know why this works but it does
it is hard for me to wrap my head around the decision, when to return a boolean or the type itself/never
You are right. We should not use „never“ because it extends everything. Just both never in CorrectStart with false. Thank you for pointing that out 🙏
@@Typed-Rocks but both never to false leads to expressions starting with "((" to be invalid
@@tngerik14 Yes, absolutely, I've updated the code on github. We need to also allow a "(" at the values of AllowedStarts: github.com/typed-rocks/typescript/blob/main/calculator.ts
my head 🤯
So was mine 😁
You basically have a mini compiler frontend.
Now you gotta make it actually compute...
Or make a piece of code that generates this based on a generic language grammar.
This way, if you manage to get the typescript syntax working, you can run typescript from a string, at compile time...
Thinking about this a bit more, if you add some sort of interface to that ts interpreter, you could produce some actual types at compile time as output of your inline ts code.
But wait, what if you run this ts compile inside of itself...
The possibilities are endless 😁
You need help xD But awesome job :)
All is lost at this point :D. But glad you liked it.
Yes... like Big Mike said "Just stop it. Get some help."
how did you check what is the result of type? it's amazing!
You mean the //^? Its a plugin for IntelliJ called „WiTT“ I‘ve created. You can download it from The Jetbrains Marketplace for free.
I see one problem.... if i'm not wrong, error message will be as "clear" just like C++ template errors...maybe not as bad but still...
100%, I would not want that in production code
Are you sure this is still TypeScript ? Maybe Prolog or Haskell or Verilog ? ;) I will stay in safe machine code / assembly space.
Maybe we can program a Haskell compiler in types? 🥹
this gives me an error:
-2+2
I will fix the code on GitHub 🙏
😎
jesus, even ai cant generate this
We should be glad. That means it can‘t create such unreadable code :P
@@Typed-Rocks im actually impressed by your abilities in writing types in typescript, good job man
@@mamad-dev thanks man, really appreciate it
Are we already entering an era in which we will take longer to write types than final code?
It‘s happening as we speak 🤘
Seeing all this magic i really don't understand this moaning of libraries authors that they have a problem with TypeScript and types... clearly just skill issue 🤣
Or maybe they want to keep their sanity 😁