how TypeScript 5.5 makes safer types in fewer keystrokes
ฝัง
- เผยแพร่เมื่อ 29 ส.ค. 2024
- TS 5.5 Announcment: devblogs.micro...
My Links
shaky.sh
shaky.sh/tools
#programming #coding #typescript #javascript
#frontenddeveloper #backenddeveloper #softwareengineer #softwareengineering #softwaredevelopment
it's so refreshing to see typescript is guessing types based on what javascript will do
Ooh this is quite nice to have indeed! I recently subscribed to the TypeScript devblogs, but I found myself not spending the time to read those... so I particularly appreciate the summary of the features you find useful. Cheers
This is amazing! I wasn't expecting all that from v5.5, just one would have sufficed lol
Thanks for keeping us updated :)
Fick Giving that predicate narrowing is such a blessing, that’s bitten me like 3 different times over the past year.
Oh the irony! I'm at 1:06 and I see your Typescript 5.4 workaround that I didn't know before JUST IN TIME for the 5.5 feature that I've been waiting for to come out and make it irrelevant. (kicks self)
I'm pretty sure obj[key] is not inferred because obj[key] can change after a call even of obj and key stay the same
Hmm, how do you mean? Wouldn't that mean this new behavior can introduce bugs?
@@andrew-burgess in a simple if(typeof === string) it can't (unless you use getters in which case normal variables could change, too [look up a == 1 && a == 2 && a == 3])
But f.ex.
if(obj[key] === 'hello') {await someLongCall(); console.log(obj[key])} could change in another function but I believe they caught that with the current implementation
@@andrew-burgess it can:
function foo(obj: Record, key: string): string {
if (typeof obj[key] === 'string') {
bar(obj, key)
return obj[key]
}
return ""
}
function bar(obj: Record, key: string) {
obj[key] = 1;
}
typechecks in 5.5, but will return 1 as a number
most languages: null is problematic, we should avoid uising it
javascript: here is null, here is undefined, null can be undefined, undefined can be null... all goes
Anybody ever told you, you look like Toby Maguire? Lol
wtf !!!
I think I will wait for typescript 9.0 when I don't have to type anything and it will be type safe. 😂😂😂😂 Telepathy compiler.
!== null doesn't work to infer out undefined it only infers out nulls.
last version works fine: typeof n === 'number'
Ah yep, I was specifically doing !=, which coerces undefined to null. But yeah, the typeof way is best for this one.
those are ligatures, got me confused for a second there as well.