Skip to content

Required, Optional and Nullable in TypeScript

fonlow edited this page Dec 18, 2023 · 2 revisions

The TypeScript codes generated by PocoToTSCore and TypeScriptCodeDom can satisfy the strict mode of TypeScript compiler and Angular Build through turning HelpStrictMode on.

OpenApiClientGen shares some core components of WebApiClientGen:

  • TypeScriptCodeDom
  • Poco2Ts

Thus the codes generated by OpenApiClientGen is consistent with what generated by WebApiClientGen even though WebApiClientGen does not need to use OpenAPI definitions.

Regarding pet.yaml, pet.ts in TypeScript are generated:

	export interface Pet {

		/** Pet ID */
		id?: number | null;

		/** Categories this pet belongs to */
		category?: Category | null;

		/**
		 * The name given to a pet
		 * Required
		 */
		name: string;

		/**
		 * The list of URL to a cute photos featuring pet
		 * Required
		 * Maximum items: 20
		 */
		photoUrls: Array<string>;
		friend?: Pet | null;

		/**
		 * Tags attached to the pet
		 * Minimum items: 1
		 */
		tags?: Array<Tag> | null;

		/** Pet status in the store */
		status?: PetStatus | null;

		/** Type of a pet */
		petType?: string | null;
	}

	export interface Category {

		/** Category ID */
		id?: number | null;

		/**
		 * Category name
		 * Min length: 1
		 */
		name?: string | null;

		/** Test Sub Category */
		sub?: CategorySub | null;
	}

	export interface CategorySub {

		/** Dumb Property */
		prop1?: string | null;
	}

	export enum PetStatus { available = 0, pending = 1, sold = 2 }


	/** A representation of a cat */
	export interface Cat extends Pet {

		/**
		 * The measured skill for hunting
		 * Required
		 */
		huntingSkill: CatHuntingSkill;
	}

	export enum CatHuntingSkill { clueless = 0, lazy = 1, adventurous = 2, aggressive = 3 }

	/** A representation of a dog */
	export interface Dog extends Pet {

		/**
		 * The size of the pack the dog is from
		 * Required
		 * Minimum: 1
		 */
		packSize: number;
	}

Summary

If a field is required, the generated:

field: FieldType;

If a field is of simple types, like string, Date, number and enum etc., the generated:

field?: FieldType | null

If a field is of Array, the generated:

field?: Array<ElementType>;

If a field of of complex types, like composited types and dictionary etc., the generated:

field?: ComplexType;
Clone this wiki locally