diff --git a/src/CodeGeneration/TypeScript.hs b/src/CodeGeneration/TypeScript.hs index d9cf7b8..5c58f67 100644 --- a/src/CodeGeneration/TypeScript.hs +++ b/src/CodeGeneration/TypeScript.hs @@ -28,7 +28,8 @@ outputModule module' = if Text.null importsOutput then "" else importsOutput <> "\n\n", declarationImportOutput, if null declarationNames then "" else "\n\n", - definitionOutput + definitionOutput, + "\n" ] modulePrelude :: Text @@ -1088,6 +1089,8 @@ unionEnumConstructorTag unionName constructorName' = mconcat [unionName, "Tag.", upperCaseFirst constructorName'] outputField :: StructField -> Text +outputField (StructField (FieldName name) (ComplexType (OptionalType fieldType))) = + mconcat [" ", name, "?: ", outputFieldType fieldType, ";\n"] outputField (StructField (FieldName name) fieldType) = mconcat [" ", name, ": ", outputFieldType fieldType, ";\n"] diff --git a/test/reference-output/arrayOfArraysOfNullableStrings.ts b/test/reference-output/arrayOfArraysOfNullableStrings.ts index a2c521c..e79d35c 100644 --- a/test/reference-output/arrayOfArraysOfNullableStrings.ts +++ b/test/reference-output/arrayOfArraysOfNullableStrings.ts @@ -10,4 +10,4 @@ export function isType(value: unknown): value is Type { export function validateType(value: unknown): svt.ValidationResult { return svt.validate(value, {field: svt.validateArray(svt.validateArray(svt.validateOptional(svt.validateString)))}); -} \ No newline at end of file +} diff --git a/test/reference-output/basic.ts b/test/reference-output/basic.ts index 26cc0c8..4bb810c 100644 --- a/test/reference-output/basic.ts +++ b/test/reference-output/basic.ts @@ -4,7 +4,7 @@ export type Recruiter = { type: "Recruiter"; Name: string; emails: (string | null | undefined)[]; - recruiter: Recruiter | null | undefined; + recruiter?: Recruiter; created: bigint; }; @@ -429,4 +429,4 @@ export function validateEmbeddedLogIn(value: unknown): svt.ValidationResult { return svt.validate(value, {type: EmbeddedEventTag.SystemImploded}); -} \ No newline at end of file +} diff --git a/test/reference-output/generics.ts b/test/reference-output/generics.ts index 582ac71..b11f31e 100644 --- a/test/reference-output/generics.ts +++ b/test/reference-output/generics.ts @@ -34,11 +34,11 @@ export function validateUsingOwnGenerics(validateT: svt.Validator): svt.Va export type KnownForMovie = { media_type: "movie"; - poster_path: string | null | undefined; + poster_path?: string; id: number; - title: string | null | undefined; + title?: string; vote_average: number; - release_date: string | null | undefined; + release_date?: string; overview: string; }; @@ -52,12 +52,12 @@ export function validateKnownForMovie(value: unknown): svt.ValidationResult } export type KnownForMovieWithoutTypeTag = { - poster_path: string | null | undefined; + poster_path?: string; id: number; - title: string | null | undefined; + title?: string; vote_average: number; - release_date: string | null | undefined; + release_date?: string; overview: string; }; @@ -96,12 +96,12 @@ export function validateKnownForMovieWithoutTypeTag(value: unknown): svt.Validat } export type KnownForShowWithoutTypeTag = { - poster_path: string | null | undefined; + poster_path?: string; id: number; vote_average: number; overview: string; - first_air_date: string | null | undefined; - name: string | null | undefined; + first_air_date?: string; + name?: string; }; export function isKnownForShowWithoutTypeTag(value: unknown): value is KnownForShowWithoutTypeTag { @@ -121,22 +121,22 @@ export enum KnownForEmbeddedTag { export type MovieStartingWithLowercase = { media_type: KnownForEmbeddedTag.MovieStartingWithLowercase; - poster_path: string | null | undefined; + poster_path?: string; id: number; - title: string | null | undefined; + title?: string; vote_average: number; - release_date: string | null | undefined; + release_date?: string; overview: string; }; export type TvStartingWithLowercase = { media_type: KnownForEmbeddedTag.TvStartingWithLowercase; - poster_path: string | null | undefined; + poster_path?: string; id: number; vote_average: number; overview: string; - first_air_date: string | null | undefined; - name: string | null | undefined; + first_air_date?: string; + name?: string; }; export function MovieStartingWithLowercase(data: KnownForMovieWithoutTypeTag): MovieStartingWithLowercase { @@ -180,22 +180,22 @@ export enum KnownForEmbeddedWithUpperCaseTag { export type Movie = { media_type: KnownForEmbeddedWithUpperCaseTag.Movie; - poster_path: string | null | undefined; + poster_path?: string; id: number; - title: string | null | undefined; + title?: string; vote_average: number; - release_date: string | null | undefined; + release_date?: string; overview: string; }; export type Tv = { media_type: KnownForEmbeddedWithUpperCaseTag.Tv; - poster_path: string | null | undefined; + poster_path?: string; id: number; vote_average: number; overview: string; - first_air_date: string | null | undefined; - name: string | null | undefined; + first_air_date?: string; + name?: string; }; export function Movie(data: KnownForMovieWithoutTypeTag): Movie { @@ -228,4 +228,4 @@ export function validateMovie(value: unknown): svt.ValidationResult { export function validateTv(value: unknown): svt.ValidationResult { return svt.validate(value, {media_type: KnownForEmbeddedWithUpperCaseTag.Tv, poster_path: svt.validateOptional(svt.validateString), id: svt.validateNumber, vote_average: svt.validateNumber, overview: svt.validateString, first_air_date: svt.validateOptional(svt.validateString), name: svt.validateOptional(svt.validateString)}); -} \ No newline at end of file +} diff --git a/test/reference-output/github.ts b/test/reference-output/github.ts index 426a575..6abb065 100644 --- a/test/reference-output/github.ts +++ b/test/reference-output/github.ts @@ -16,8 +16,8 @@ export type UserData = { following: number; created_at: string; updated_at: string; - location: string | null | undefined; - blog: string | null | undefined; + location?: string; + blog?: string; }; export function isUserData(value: unknown): value is UserData { @@ -51,9 +51,9 @@ export type OrganizationData = { login: string; id: number; avatar_url: string; - members_url: string | null | undefined; + members_url?: string; repos_url: string; - description: string | null | undefined; + description?: string; }; export function isOrganizationData(value: unknown): value is OrganizationData { @@ -88,9 +88,9 @@ export type Organization = { login: string; id: number; avatar_url: string; - members_url: string | null | undefined; + members_url?: string; repos_url: string; - description: string | null | undefined; + description?: string; }; export function User(data: OwnerData): User { @@ -133,11 +133,11 @@ export type Repository = { fork: boolean; created_at: string; updated_at: string; - description: string | null | undefined; + description?: string; owner: Owner; url: string; html_url: string; - language: string | null | undefined; + language?: string; }; export function isRepository(value: unknown): value is Repository { @@ -203,12 +203,12 @@ export type Issue = { labels: Label[]; state: string; locked: boolean; - assignee: UserData | null | undefined; + assignee?: UserData; assignees: UserData[]; comments: number; created_at: string; updated_at: string; - closed_at: string | null | undefined; + closed_at?: string; author_association: string; body: string; }; @@ -310,4 +310,4 @@ export function isRepositorySearchData(value: unknown): value is RepositorySearc export function validateRepositorySearchData(value: unknown): svt.ValidationResult { return svt.validate(value, {total_count: svt.validateNumber, incomplete_results: svt.validateBoolean, items: svt.validateArray(validateRepository)}); -} \ No newline at end of file +} diff --git a/test/reference-output/hasGeneric.ts b/test/reference-output/hasGeneric.ts index 2e5b3e3..b3c9cb8 100644 --- a/test/reference-output/hasGeneric.ts +++ b/test/reference-output/hasGeneric.ts @@ -152,4 +152,4 @@ export function validateGenericEvent(validateT: svt.Validator): svt.Valida return function validateGenericEventT(value: unknown): svt.ValidationResult> { return svt.validate>(value, {type: HasGenericEventTag.GenericEvent, data: external.validateOption(validateT)}); }; -} \ No newline at end of file +} diff --git a/test/reference-output/importExample.ts b/test/reference-output/importExample.ts index a64550b..7c10d4d 100644 --- a/test/reference-output/importExample.ts +++ b/test/reference-output/importExample.ts @@ -102,4 +102,4 @@ export function isAllConcrete(value: unknown): value is AllConcrete { export function validateAllConcrete(value: unknown): svt.ValidationResult { return svt.validate(value, {field: validateHoldsSomething(basic.validateEither(basic.validateMaybe(validateStructureUsingImport), validateUnionUsingImport))}); -} \ No newline at end of file +}