-
Notifications
You must be signed in to change notification settings - Fork 710
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #1312
- Loading branch information
Showing
9 changed files
with
236 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Type } from "./abstract"; | ||
import { UnionType } from "./union"; | ||
import { IntersectionType } from "./intersection"; | ||
|
||
/** | ||
* Represents an optional type | ||
* ```ts | ||
* type Z = [1, 2?] | ||
* // ^^ | ||
* ``` | ||
*/ | ||
export class OptionalType extends Type { | ||
/** | ||
* The type of the rest array elements. | ||
*/ | ||
elementType: Type; | ||
|
||
/** | ||
* The type name identifier. | ||
*/ | ||
readonly type = "optional"; | ||
|
||
/** | ||
* Create a new OptionalType instance. | ||
* | ||
* @param elementType The type of the element | ||
*/ | ||
constructor(elementType: Type) { | ||
super(); | ||
this.elementType = elementType; | ||
} | ||
|
||
/** | ||
* Clone this type. | ||
* | ||
* @return A clone of this type. | ||
*/ | ||
clone(): Type { | ||
return new OptionalType(this.elementType.clone()); | ||
} | ||
|
||
/** | ||
* Test whether this type equals the given type. | ||
* | ||
* @param type The type that should be checked for equality. | ||
* @returns TRUE if the given type equals this type, FALSE otherwise. | ||
*/ | ||
equals(type: Type): boolean { | ||
if (!(type instanceof OptionalType)) { | ||
return false; | ||
} | ||
return type.elementType.equals(this.elementType); | ||
} | ||
|
||
/** | ||
* Return a string representation of this type. | ||
*/ | ||
toString() { | ||
if ( | ||
this.elementType instanceof UnionType || | ||
this.elementType instanceof IntersectionType | ||
) { | ||
return `(${this.elementType.toString()})?`; | ||
} | ||
return `${this.elementType.toString()}?`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { OptionalType } from "../../../models"; | ||
|
||
import { TypeSerializerComponent } from "../../components"; | ||
import { OptionalType as JSONOptionalType } from "../../schema"; | ||
|
||
export class OptionalTypeSerializer extends TypeSerializerComponent<OptionalType> { | ||
supports(t: unknown) { | ||
return t instanceof OptionalType; | ||
} | ||
|
||
/** | ||
* Will be run after [[TypeSerializer]] so `type` will already be set. | ||
* @param type | ||
* @param obj | ||
*/ | ||
toObject( | ||
type: OptionalType, | ||
obj: Pick<JSONOptionalType, "type"> | ||
): JSONOptionalType { | ||
return { | ||
...obj, | ||
elementType: this.owner.toObject(type.elementType), | ||
}; | ||
} | ||
} |
Oops, something went wrong.