Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: string defaults, chained index access, format subscope #1024

Merged
merged 76 commits into from
Jun 23, 2024

Conversation

ssalbdivad
Copy link
Member

@ssalbdivad ssalbdivad commented Jun 23, 2024

String defaults

Previously, setting a default value on an object required a tuple expression:

const myType = type({ value: ["number", "=", 42] })

This is still valid, but now a more convenient syntax is supported for many common cases:

const myType = type({ value: "number = 42" })

The original syntax is still supported, and will be required for cases where the default value is not a serializable primitive e.g.

const mySymbol = Symbol()
const myType = type({ value: ["symbol", "=", mySymbol] })

Chained index access

This allows type-safe chained index access on types via a .get method

const myUnion = type(
	{
		foo: {
			bar: "0"
		}
	},
	"|",
	{
		foo: {
			bar: "1"
		}
	}
)

// Type<0 | 1>
const fooBar = myUnion.get("foo", "bar")

format subscope keyword

The new built-in format subscope contains keywords for transforming validated strings:

const foo = " fOO "

const trim = type("format.trim")
// "fOO"
console.log(trim(foo))

const lowercase = type("format.lowercase")
// " foo "
console.log(lowercase(foo))

const uppercase = type("format.uppercase")
// " FOO "
console.log(uppercase(foo))

Many more improvements, especially related to morphs across unions

@ssalbdivad ssalbdivad merged commit 5284b60 into main Jun 23, 2024
6 checks passed
@ssalbdivad ssalbdivad deleted the string-defaults branch June 23, 2024 01:00
@github-actions github-actions bot mentioned this pull request Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done (merged or closed)
Development

Successfully merging this pull request may close these issues.

None yet

1 participant