Skip to content

Conversation

@sehrope
Copy link

@sehrope sehrope commented Aug 6, 2025

Improves the type signature of the stringify function to take into account the main argument that we are serializing. If it is a primitive scalar, array, or object, and there is no second argument then the return value will always be a string.

As this is the common case for user applications, this improves the user experience as we do not have to check the result to ensure the value is defined. For most use cases where the callsite has a primitive, array, or object and is using the default serilaization options, the return value will be known to be a string

This likely addresses most real world issues with #19 as only those with custom options would get string | undefined as the return type.


If you want to try this out, enable strict null checks in tsconfig.json and then try this:

import stringify from '../index';

const numberReturnsString: string = stringify(123);
const objectReturnsString: string = stringify({});
const object2ReturnsString: string = stringify({ foo: 123 });
const object3ReturnsString: string = stringify({ 123: "foo" });
const booleanReturnsString: string = stringify(true);
const nullReturnsString: string = stringify(null);
const arrayReturnsString: string = stringify([]);
const undefinedReturnsUndefined: undefined = stringify(undefined);
const unknownReturnsUnknown: string | undefined = stringify(undefined as any);

Improves the type signature of the stringify function to take into account the main argument
that we are serializing. If it is a primitive scalar, array, or object, and there is no second
argument then the return value will always be a string.

As this is the common case for user applications, this improves the user experience as we do
not have to check the result to ensure the value is defined. For most use cases where the
callsite has a primitive, array, or object and is using the default serilaization, the
return value will be known to be a string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant