nicojs / typed-html

TypeSafe HTML templates using TypeScript. No need to learn a template library.
333 stars 51 forks source link

required bug / string|undefined -> boolean #49

Open mrinc opened 7 months ago

mrinc commented 7 months ago

Ref: https://github.com/nicojs/typed-html/blob/88a6a6219bbf4d9368537ee29419d29671573093/src/jsx/element-types.d.ts#L150

As this is a string|undefined, it denotes required or not defined as per html spec... but...
If you pass in undefined the following error occurs:

https://github.com/nicojs/typed-html/blob/88a6a6219bbf4d9368537ee29419d29671573093/src/elements.tsx#L66

TypeError: Cannot read properties of undefined (reading 'toString')

2 step recommendation:

https://github.com/nicojs/typed-html/blob/88a6a6219bbf4d9368537ee29419d29671573093/src/jsx/element-types.d.ts#L150
From: required?: string;
To: required?: boolean;

https://github.com/nicojs/typed-html/blob/88a6a6219bbf4d9368537ee29419d29671573093/src/elements.tsx#L66
From:

return makeAttribute(escapeAttrNodeValue(value.toString()));

To:

return makeAttribute(value !== undefined && value !== null && typeof value.toString === 'function' ? escapeAttrNodeValue(value.toString()) : '');

Happy to push a PR for this, let me know.