Real-world uses of TypeScript’s utility types
Utility types are types that modify other types. You can think of them as functions, but they operate on types instead of values.
Read more [piccalil.li]
Posts tagged with typing
Utility types are types that modify other types. You can think of them as functions, but they operate on types instead of values.
Read more [piccalil.li]
– liamduckett.com - submitted by Liam
A short post about my preferences around type safety and static analysis.
Read more [liamduckett.com]
Join 9,500+ smart developers
Every month I share what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.
No spam. Unsubscribe anytime. You can also follow me on X.
Pseudo-types like callable and iterable may seem convenient at first glance, but they introduce ambiguities and make code harder to analyze.
Read more [f2r.github.io]
Generics have been on the list of wanted features for a long time by numerous PHP developers. This article explores the different approaches, and what their current state is.
Read more [thephp.foundation]
This is how Seb decides when or when not to explicitly add types in TypeScript.
Read more [sebastiandedeyne.com]
The usage of phantom types is a simple type level trick which can help with this in practical settings, without requiring complicated features or making your code too abstract and less readable.
Read more [marcosh.github.io]
Leonel Elimpe explains how you can use our DTO package.
Read more [github.com]
Brent wrote another nice post on PHP. This time on the upcoming typed hints feature of PHP 7.4.
Typed class properties were added in PHP 7.4 and provide a major improvement to PHP's type system. These changes are fully opt-in and backwards compatible. In this post we'll look at the feature in-depth.
Read more [stitcher.io]
In an older but still relevant blogpost, Ondřej Mirtes, the author of PHPStan, explains the difference between union types and intersection types.
One of the headlining features of PHPStan 0.9 is the introduction of intersection types. Since this is a very useful feature that helps us understand the code much better, but the terminology is largely unknown and mysterious to the PHP community, I decided to write up and compare these two kinds of compound types.
Read more [medium.com]
David Négrier, CTO of the CodingMachine, wrote a nice article on why he likes and how his team uses typehints.
As a developer consuming thisfunction, I know how to use it. And if I’m using it wrong, I’ll know right away because PHP will crash with a nice error message when the function is called rather than with a cryptic error some time later.
https://www.thecodingmachine.com/type-hint-all-the-things/
Personally I like typehints too, because the potential readability improvement the article touches upon.
Note: (I only include this paragraph because it's mentioned in the intro of the article, don't want to stir up a discussion) the fuzz about that "Visual Debt" video was overblown. Even though I didn't agree with all of it, it was nice to hear Jeffrey's way of thinking.
You might thing that PHP is not able to automatically perform a type check on items in an array. But using variadic constructor this is possible. Bert Ramakers wrote a blogpost with some good examples on how to do this.
One of the language features announced back in PHP 5.6 was the addition of the “…” token to denote that a function or method accepts a variable length of arguments.Something I rarely see mentioned is that it’s possible to combine this feature with type hints to essentially create typed arrays.
The venerable Uncle Bob makes the case for dynamic typing and TDD. Be sure to read the entire article to get a quick history lesson in computer languages.
The pendulum is quickly swinging towards dynamic typing. Programmers are leaving the statically typed languages like C++, Java, and C# in favor of the dynamically typed languages like Ruby and Python. And yet, the new languages that are appearing, languages like go and swift appear to be reasserting static typing? So is the stage for the next battle being set?http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.htmlHow will this all end?
My own prediction is that TDD is the deciding factor. You don't need static type checking if you have 100% unit test coverage. And, as we have repeatedly seen, unit test coverage close to 100% can, and is, being achieved. What's more, the benefits of that achievement are enormous.
Tim Bezhashvyly recently wrote an article in which he explains an interesting approach to make sure all items in array are of a certain type. It leverages variadic functions which were introduced in PHP 5.6
Consider this piece of code (borrowed from Tim's post):
function foo (Product ...$products )
{
/* ... */
}
In PHP 7 you can even using the scalar types, such as string and int, to make sure all elements are of that type.
Read Tim's full article here: https://thephp.cc/news/2016/02/typed-arrays-in-php