|
|
|
|
|
by AltruisticGapHN
1670 days ago
|
|
Ok say I have a small class with an options object (I know perhaps not the best design...): class Foo {
public function __construct(string $name, array $options)
{
$this->name = $name;
if ($options->enableFlag) { ...
I want to declare what $options are, in TS I could do:type TFooOptions = {
enableFlag: boolean;
userIds: number[];
} Then function __construct(string $name, TFooOptions $options) ...
Otherwise how do I get proper typechecking if the IDE doesn't know what $options are? |
|
is that creating a JS class under the hood or is just a hint for the compiler?
What I do , but I don't work with recent PHP versions is to create this as a class, then to make this easy to construct objects from JSON i add a static function fromObject or fromJson that does it for me. Using real classes could prevent bugs when working with shit APIs like Microsoft TTS , where they have 2 endponts and one returns objects like voice: {name:"Bob"} and the other voice: {Name:"Bob"} so my code will wrap both type of responses into a well defined class.
I don't think there is a PHPDoc annotation just to hint what that object looks like but I will be happey to be shown a way.