Skip to content

Commit

Permalink
Merge pull request #482 from stepchowfun/optional-undefined
Browse files Browse the repository at this point in the history
Require optional struct fields to be explicitly set to `undefined` in TypeScript to avoid setting them
  • Loading branch information
stepchowfun authored Jun 19, 2024
2 parents 96bf966 + 8c128ac commit 9cdf0a1
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 146 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.11.0] - 2024-06-18

### Changed
- Typical now requires optional struct fields to be explicitly set to `undefined` in TypeScript to avoid setting them. Previously, such fields could be omitted entirely, but that meant typos in optional field names could go unnoticed.

## [0.10.0] - 2024-06-13

### Added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "typical"
version = "0.10.0"
version = "0.11.0"
authors = ["Stephan Boyer <[email protected]>"]
edition = "2021"
description = "Data interchange with algebraic data types."
Expand Down
8 changes: 4 additions & 4 deletions src/generate_typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1367,20 +1367,20 @@ fn write_struct<T: Write>(
for field in fields {
write_indentation(buffer, indentation + 1)?;
write_identifier(buffer, &field.name, Camel, None)?;
write!(buffer, ": ")?;
write_type(buffer, imports, namespace, &field.r#type.variant, direction)?;
match field.rule {
schema::Rule::Asymmetric => match direction {
Direction::Atlas | Direction::Out => {}
Direction::In => {
write!(buffer, "?")?;
write!(buffer, " | undefined")?;
}
},
schema::Rule::Optional => {
write!(buffer, "?")?;
write!(buffer, " | undefined")?;
}
schema::Rule::Required => {}
}
write!(buffer, ": ")?;
write_type(buffer, imports, namespace, &field.r#type.variant, direction)?;
writeln!(buffer, ";")?;
}

Expand Down
Loading

0 comments on commit 9cdf0a1

Please sign in to comment.