mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2024-11-22 00:11:02 -05:00
Implemented class that represents type alias
This commit is contained in:
parent
cec0b14ff4
commit
755c51fa61
1 changed files with 104 additions and 0 deletions
104
src/utils/typescript/typescript-type-alias.ts
Normal file
104
src/utils/typescript/typescript-type-alias.ts
Normal file
|
@ -0,0 +1,104 @@
|
|||
import { AbstractTypeScriptNode } from "./abstract-typescript-node";
|
||||
import { getIndentation, TypeScriptFormattingOptions } from "./typescript-formatting-options";
|
||||
import { TypeScriptTypeDefinition } from "./typescript-type-definition";
|
||||
|
||||
/**
|
||||
* Represents a type alias in a TypeScript module.
|
||||
*/
|
||||
export class TypeScriptTypeAlias extends AbstractTypeScriptNode implements TypeScriptTypeDefinition {
|
||||
/**
|
||||
* The name of the type alias.
|
||||
*/
|
||||
private readonly _name: string;
|
||||
|
||||
/**
|
||||
* The type definition of the type alias.
|
||||
*/
|
||||
private readonly _type: TypeScriptTypeDefinition;
|
||||
|
||||
/**
|
||||
* Constructs a new {@link TypeScriptTypeAlias} instance.
|
||||
*
|
||||
* @param name - The name of the type alias.
|
||||
* @param type - The type definition of the type alias.
|
||||
*/
|
||||
private constructor(name: string, type: TypeScriptTypeDefinition) {
|
||||
super();
|
||||
this._name = name;
|
||||
this._type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link TypeScriptTypeAlias} instance.
|
||||
*
|
||||
* @param name - The name of the type alias.
|
||||
* @param type - The type definition of the type alias.
|
||||
*
|
||||
* @returns A new {@link TypeScriptTypeAlias} instance.
|
||||
*/
|
||||
static create(name: string, type: TypeScriptTypeDefinition): TypeScriptTypeAlias {
|
||||
return new TypeScriptTypeAlias(name, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the type alias.
|
||||
*/
|
||||
get name(): string {
|
||||
return this._name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the type definition of the type alias.
|
||||
*/
|
||||
get type(): TypeScriptTypeDefinition {
|
||||
return this._type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
get isComposite(): true {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
get isUnion(): false {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
get isIntersection(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
get isAlias(): true {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
composingTypes(): Iterable<TypeScriptTypeDefinition> {
|
||||
return [this._type];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
formatContent(options?: TypeScriptFormattingOptions): string {
|
||||
const indent = getIndentation(options);
|
||||
|
||||
const formattedName = this._name;
|
||||
const formattedType = this._type.format(options).trimStart();
|
||||
const formattedTypeAlias = `${indent}type ${formattedName} = ${formattedType};`;
|
||||
|
||||
return formattedTypeAlias;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue