Commit 4d7ce3ac authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

ts: définit et utilise un typeguard pour vérifier la présence d'une propriété.

parent 68ac33fc
...@@ -13,6 +13,11 @@ import { {% for name in repoImports %} ...@@ -13,6 +13,11 @@ import { {% for name in repoImports %}
{%- endfor %} {%- endfor %}
} from './resources'; } from './resources';
// Simple type guard helper for object with keys
function hasProperty<T extends string|number|symbol>(what: unknown, key: T): what is { [key in T]: any; } {
return typeof what === 'object' && what !== null && key in what;
}
/********************************************************************************* /*********************************************************************************
* Repositories * Repositories
*********************************************************************************/ *********************************************************************************/
......
...@@ -66,7 +66,7 @@ final class TypescriptHelper ...@@ -66,7 +66,7 @@ final class TypescriptHelper
*/ */
public static function propertyTestor(string $object, string $name): string public static function propertyTestor(string $object, string $name): string
{ {
return sprintf('(%s in %s)', self::quoteString($name), $object); return sprintf('hasProperty(%s, %s)', $object, self::quoteString($name));
} }
/** /**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment