Hacker News new | ask | show | jobs
by tyingq 1670 days ago
It's not clear to me what you're looking for. But you can cast arrays to objects. And you can use type declarations as arguments for functions.
1 comments

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?
>type TFooOptions

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.

Simple object type in TypeScript, the "type checking" abstraction layer on top of JS, which actually doesn't even need to be compiled (eg. esbuild strips all typing from the code before compiling).

https://www.typescriptlang.org/docs/handbook/2/everyday-type...

Your approach is sensible. I remember using json_encode() indeed so I could declare some data in a php file with <<< HEREDOC in the Javascript short syntax (though short array syntax in php nowadays makes it less painful, JSON syntax still a bit less verbose).

I checked that example in TS playground and it does nothing when transpiling, so is only a compile type check. From my experience when I have an option object with many properties I would create a class both in PHP and JavaScript, usually functions that need an options object are very complex and many times you need to add even more options and having a class make it simple to do the changes and refactor.
You can get some basic checking, but not type in arrays [1]. Maybe using attributes you could check if it is?

  class TFooOptions
  {
      public function __construct(
          public bool $enableFlag,
          public array $userIds = [],
      ) {
      }
  }

  $options = new TFooOptions(
      enableflags: true,
      userIds: [1,2,3]
  );

  new Foo('Bob', $options);
Or if just for IDE checking, you could use the @property docblock value on the class.

[1] https://wiki.php.net/rfc/arrayof

PHP doesn't do such structural typing, but Psalm[1] can do it statically. It calls them object-like arrays.[2]

[1]: https://psalm.dev/ [2]: https://psalm.dev/docs/annotating_code/type_syntax/array_typ...

You can force a type check that it's a specific type of object or interface rather than an array. But, no, you can't do what you're asking for directly.