withastro/astro · error · TypeError
First argument must be a string
Error message
First argument must be a string
What it means
`parseInlineStyles` is a small hand-written CSS inline-style parser. Its contract requires the first argument be a string; any other type (number, undefined, null, object) throws `TypeError('First argument must be a string')`. An empty string short-circuits to `[]`.
Source
Thrown at packages/integrations/markdoc/src/html/css/parse-inline-styles.ts:55
const NEWLINE = '\n';
const FORWARD_SLASH = '/';
const ASTERISK = '*';
const EMPTY_STRING = '';
// types
const TYPE_COMMENT = 'comment';
const TYPE_DECLARATION = 'declaration';
/**
* @param {String} style
* @param {Object} [options]
* @return {Object[]}
* @throws {TypeError}
* @throws {Error}
*/
export function parseInlineStyles(style, options) {
if (typeof style !== 'string') {
throw new TypeError('First argument must be a string');
}
if (!style) return [];
options = options || {};
/**
* Positional.
*/
let lineno = 1;
let column = 1;
/**
* Update lineno and column based on `str`.
*
* @param {String} str
*/
function updatePosition(str) {View on GitHub (pinned to d081033d5f)
Solutions
- Guard the call: `if (typeof style === 'string') parseInlineStyles(style)`.
- Coerce with a default: `parseInlineStyles(style ?? '')` (empty string returns `[]`).
- Fix the upstream caller to only pass strings.
Example fix
// before parseInlineStyles(node.attributes.style) // after parseInlineStyles(typeof node.attributes.style === 'string' ? node.attributes.style : '')
Defensive patterns
Strategy: type-guard
Validate before calling
function safeParseInlineStyles(style: unknown) {
return typeof style === 'string' ? parseInlineStyles(style) : [];
} Type guard
function isStyleString(v: unknown): v is string {
return typeof v === 'string';
} Prevention
- Always type the input as `string` in your wrapper.
- Coerce optional attributes with `?? ''` before passing.
- Unit-test the parser with undefined/null inputs.
When it happens
Trigger: Calling `parseInlineStyles(value)` where `value` is `undefined`, `null`, a number, an object, or any non-string. Common when reading an attribute that may be absent (`element.attributes.style` returning undefined) and passing it through without coercion.
Common situations: Markdoc/MDX node whose `style` attribute is optional and not always present. Passing a parsed AST node instead of its string value. Defensive code path that forgets a null check.
Related errors
- CannotDetermineWeightAndStyleFromFontFile
- ${cssTransformErrors[0].message}
- Invalid transition name {${transitionName}}
- (Uncaught error) Partial tag requires a 'file' attribute
- **${relativePartialPath}** contains invalid content: Could n
AI-assisted analysis of withastro/astro@d081033d5f (2026-08-12).
Data as JSON: /api/errors/ac68c0161c1678bb.
Report an issue: GitHub.