Commit f25e60af authored by Guillaume Perréal's avatar Guillaume Perréal

Améliore les type-checks et casts des types "built-ins".

Notamment pour true, false, undefined et null qui sont des singletons.
parent fc306ce6
......@@ -52,15 +52,16 @@ class BuiltinType extends AbstractType
case 'number':
case 'boolean':
return sprintf('%s.toString()', $expr);
case 'null':
case 'undefined':
case 'true':
case 'false':
return sprintf('"%s"', $this->name);
case 'Date':
return sprintf('%s.toISOString()', $expr);
case 'undefined':
case 'null':
case 'never':
return '';
case 'unknown':
return sprintf('"" /* %s */', $this->name);
default:
return sprintf('JSON.stringify(%s)', $expr);
}
......@@ -71,6 +72,19 @@ class BuiltinType extends AbstractType
*/
public function checkType(string $expr, bool $explicit = false): string
{
return sprintf('(typeof %s === %s)', $expr, TypescriptHelper::quoteString($this->name));
switch ($this->name) {
case 'null':
case 'true':
case 'false':
case 'undefined':
return sprintf('(%s === %s)', $expr, $this->name);
case 'unknown':
case 'any':
return 'true';
case 'Date':
return sprintf('(%s instanceof %s)', $expr, $this->name);
default:
return sprintf('(typeof %s === %s)', $expr, TypescriptHelper::quoteString($this->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