Skip to content

Commit

Permalink
🔨 Fix: Generic Types
Browse files Browse the repository at this point in the history
- unions, intersections of generic types, added custom parameters to deep generic types.
- Added tests for generated guards
  • Loading branch information
rohit1901 authored Aug 24, 2023
2 parents e01a287 + 0492cff commit 25c24e6
Show file tree
Hide file tree
Showing 40 changed files with 1,554 additions and 202 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

## 1.0.7 - 2023-08-22
## 1.0.8 - 2023-08-24

### Commits

- Merge 300760bcf186f5091b104de7b80211f03c8f58b4 into 9b82959dd1e5fcf0a4d2f3b0e098796adc92d672 [`7d2b774`](https://github.com/rohit1901/ts-gen-typeguards/commit/7d2b77466fd72ab4bd9868d39ce6987a534a34f8)
- Merge e57298cbbf8c1dc17befbb5bda1ff825767d3416 into e01a287e6b7726693e38edd527cebbef64239fdc [`f79ea31`](https://github.com/rohit1901/ts-gen-typeguards/commit/f79ea3147d50284bbae7a5d62c4230c00288e94f)
80 changes: 65 additions & 15 deletions input/combinedTypeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export enum TrafficLightEnum {
}

// Using enums in object properties
interface CarEnum {
export interface CarEnum {
make: string;
model: string;
color: ColorEnum;
Expand All @@ -70,6 +70,65 @@ export type ExtendedDirectionEnum = DirectionEnum & 'FORWARD' & 'BACKWARD';



// Generic interface
export interface Container<T> {
value: T;
}
export type ContainerOfString = Container<string>;
export type ContainerOfNumber = Container<number>;

// Generic type from enum
export enum ColorEnumGeneric {
Red = 'RED',
Green = 'GREEN',
Blue = 'BLUE',
}

export type ColorContainer<T extends ColorEnumGeneric> = Container<T>;

export type RedContainer = ColorContainer<ColorEnumGeneric.Red>;
export type GreenContainer = ColorContainer<ColorEnumGeneric.Green>;
export type BlueContainer = ColorContainer<ColorEnumGeneric.Blue>;

// Generic interface with optional property
export interface PropertyContainer<T> {
value: T;
additionalValue?: T;
}

// Derived types with optional property
export type OptionalContainerOfString = PropertyContainer<string>;
export type OptionalContainerOfNumber = PropertyContainer<number>;

// Generic type from union of literals
export type DirectionGeneric = 'up' | 'down' | 'left' | 'right';

export type DirectionContainer<T extends DirectionGeneric> = Container<T>;

export type UpDirectionContainer = DirectionContainer<'up'>;
export type DownDirectionContainer = DirectionContainer<'down'>;

// Generic type with intersection
export interface ShapeGeneric {
type: 'circle' | 'square' | 'triangle';
}

export type ShapeWithProperty<T extends ShapeGeneric> = T & { color: ColorEnumGeneric };

export type ColoredCircle = ShapeWithProperty<{ type: 'circle'; radius: number }>;
export type ColoredSquare = ShapeWithProperty<{ type: 'square'; sideLength: number }>;

// Generic type with intersection from enum
export enum VehicleTypeGeneric {
Car = 'CAR',
Bike = 'BIKE',
}

export type VehicleWithType<T extends VehicleTypeGeneric> = { type: T; brand: string; model: string };

export type CarWithInfo = VehicleWithType<VehicleTypeGeneric.Car>;
export type BikeWithInfo = VehicleWithType<VehicleTypeGeneric.Bike>;

export interface AddressInterface {
street: string;
city: string;
Expand Down Expand Up @@ -101,7 +160,7 @@ export interface TriangleInterface {
height: number;
}

type CustomShapeInterface = CircleInterface | SquareInterface | TriangleInterface;
export type CustomShapeInterface = CircleInterface | SquareInterface | TriangleInterface;

export interface CarInterface {
brand: string;
Expand Down Expand Up @@ -261,15 +320,6 @@ export type EmployeeType = {
jobTitle: string;
} & PersonType;

const john: EmployeeType = {
name: 'John',
age: 30,
jobTitle: 'Developer',
address: {
street: '123 Main St',
city: 'Cityville',
},
};
//Union Types
export type NumberOrStringType = number | string;

Expand Down Expand Up @@ -317,23 +367,23 @@ export type DataType = {

export type VehicleType = CarType | ElectricCarType | SportsCar;

enum ColorTypeAliasType {
export enum ColorTypeAliasType {
Red = 'RED',
Green = 'GREEN',
Blue = 'BLUE',
}

enum MediaTypeType {
export enum MediaTypeType {
Image = 'image',
Video = 'video',
}

enum ImageTypeType {
export enum ImageTypeType {
JPEG = 'jpeg',
PNG = 'png',
}

enum VideoType {
export enum VideoType {
MP4 = 'mp4',
AVI = 'avi',
}
Expand Down
2 changes: 1 addition & 1 deletion input/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export enum TrafficLightEnum {
}

// Using enums in object properties
interface CarEnum {
export interface CarEnum {
make: string;
model: string;
color: ColorEnum;
Expand Down
58 changes: 58 additions & 0 deletions input/generics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Generic interface
export interface Container<T> {
value: T;
}
export type ContainerOfString = Container<string>;
export type ContainerOfNumber = Container<number>;

// Generic type from enum
export enum ColorEnumGeneric {
Red = 'RED',
Green = 'GREEN',
Blue = 'BLUE',
}

export type ColorContainer<T extends ColorEnumGeneric> = Container<T>;

export type RedContainer = ColorContainer<ColorEnumGeneric.Red>;
export type GreenContainer = ColorContainer<ColorEnumGeneric.Green>;
export type BlueContainer = ColorContainer<ColorEnumGeneric.Blue>;

// Generic interface with optional property
export interface PropertyContainer<T> {
value: T;
additionalValue?: T;
}

// Derived types with optional property
export type OptionalContainerOfString = PropertyContainer<string>;
export type OptionalContainerOfNumber = PropertyContainer<number>;

// Generic type from union of literals
export type DirectionGeneric = 'up' | 'down' | 'left' | 'right';

export type DirectionContainer<T extends DirectionGeneric> = Container<T>;

export type UpDirectionContainer = DirectionContainer<'up'>;
export type DownDirectionContainer = DirectionContainer<'down'>;

// Generic type with intersection
export interface ShapeGeneric {
type: 'circle' | 'square' | 'triangle';
}

export type ShapeWithProperty<T extends ShapeGeneric> = T & { color: ColorEnumGeneric };

export type ColoredCircle = ShapeWithProperty<{ type: 'circle'; radius: number }>;
export type ColoredSquare = ShapeWithProperty<{ type: 'square'; sideLength: number }>;

// Generic type with intersection from enum
export enum VehicleTypeGeneric {
Car = 'CAR',
Bike = 'BIKE',
}

export type VehicleWithType<T extends VehicleTypeGeneric> = { type: T; brand: string; model: string };

export type CarWithInfo = VehicleWithType<VehicleTypeGeneric.Car>;
export type BikeWithInfo = VehicleWithType<VehicleTypeGeneric.Bike>;
2 changes: 1 addition & 1 deletion input/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface TriangleInterface {
height: number;
}

type CustomShapeInterface = CircleInterface | SquareInterface | TriangleInterface;
export type CustomShapeInterface = CircleInterface | SquareInterface | TriangleInterface;

export interface CarInterface {
brand: string;
Expand Down
17 changes: 4 additions & 13 deletions input/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ export type EmployeeType = {
jobTitle: string;
} & PersonType;

const john: EmployeeType = {
name: 'John',
age: 30,
jobTitle: 'Developer',
address: {
street: '123 Main St',
city: 'Cityville',
},
};
//Union Types
export type NumberOrStringType = number | string;

Expand Down Expand Up @@ -70,23 +61,23 @@ export type DataType = {

export type VehicleType = CarType | ElectricCarType | SportsCar;

enum ColorTypeAliasType {
export enum ColorTypeAliasType {
Red = 'RED',
Green = 'GREEN',
Blue = 'BLUE',
}

enum MediaTypeType {
export enum MediaTypeType {
Image = 'image',
Video = 'video',
}

enum ImageTypeType {
export enum ImageTypeType {
JPEG = 'jpeg',
PNG = 'png',
}

enum VideoType {
export enum VideoType {
MP4 = 'mp4',
AVI = 'avi',
}
Expand Down
Loading

0 comments on commit 25c24e6

Please sign in to comment.