It's Sunday in New Zealand and I don't want to get out of bed yet, so instead I'm going to listen to the new Menzingers album and learn about TypeS...
For further actions, you may consider blocking this person and/or reporting abuse
Seriously amazing article. I wish there was more legible content like this about advanced Typescript types. I thought your approach of using the typefunction made it especially easy to understand. Have you considered opening an RFC on the typefunction syntax? It’s a really interesting idea.
I would love to read more content like this, and encourage you to keep contributing.
Thanks for the great article.
I was messing around with the string dot Path type, I think it can be made a little simpler by splitting it into two types
Subpath gets the paths for properties of T, and Path stitches them all together into a single type.
PathType can also be made a lot more concise by using type intersection on Key and Rest
I'm not sure if this is easier to understand than the original though.
Here's the result.
This is great stuff!
I have been using your improved types for a better part of this last year, and I came into an example of a type that breaks the interface, and I honestly have no idea why. (Also doesn't work with original type)
Counter example of the interface not correctly idenying
foo.test
@nroboto do you know what's going on here? Am i missing something?
The issue is that in the
Subpath
type we exclude all of the properties of an array, which includes thelength
property, this results infoo.length
being excluded even though in this caselength
is a property on an object. One way to fix this is to change the exclusion to only apply if the type is an array:Here's the TS playground.
Hey Craig, these new TypeScript functionalities are blowing my mind and your article was amazing, really appreciated how you broke down the fairly hard to digest type definitions into smaller steps to help follow the thought process. :)
I have a nitpick, there seems to be a difference in your rewrite of the type-safe string dot notation example. Unlike the original code example, it will not accept top-level properties:
Nice catch! I'm not surprised that I broke something! Hopefully people don't use my version for real, as it was strictly for me trying to work it all out 😅
Hey Craig, when I use
Interface
, it is not working onArray.Number
here is the code
Looking forward to Ur help
Thx to @nroboto . Finally I've solved type-safe array dot notation.
here is the code-intelligence
See here
Update for derivation of array types
type SubArrayPath
= P extends PropertyKey ?
${number}
:${number}
|${number}.${PathTemp<P>}
One question about
PathValue
. With your implementation above, we getnever
withget(user, 'projects.0.name')
.But it will get the correct type with one small change:
See here.
Wowza. Gonna have TS written in TS Types soon.
Thanks for this great article!
Awesome article!
with great power comes great responsibility :)))
Thanks for this article, I learn a lot with your explanation. good album too :-)
I'll keep it in my favorite as a manual to understand template literal type