This repository has been archived by the owner on Feb 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
type-declaration.ts
55 lines (49 loc) · 1.5 KB
/
type-declaration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import * as ts from 'typescript';
import { IType } from '../collections';
import { any_type, ElementKind } from '../constants';
import {
create_element,
is_element,
IElement,
IElementOptions,
} from '../element';
import { transform } from '../transform';
import { create_type_parameters } from '../utils';
import { IGenericDeclaration } from './generic-declaration';
export interface ITypeDeclarationOptions extends IElementOptions {
name: string;
export?: boolean;
generics?: IGenericDeclaration[];
type?: IType;
}
export interface ITypeDeclaration
extends IElement<ElementKind.TypeDeclaration>,
ITypeDeclarationOptions {}
export const create_type_declaration = (
options: ITypeDeclarationOptions,
): ITypeDeclaration => ({
...create_element(ElementKind.TypeDeclaration),
...options,
});
export const is_type_declaration = (value: any): value is ITypeDeclaration =>
is_element(value) && value.kind === ElementKind.TypeDeclaration;
// tslint:disable:ter-indent
/**
* @hidden
*/
export const transform_type_declaration = (
element: ITypeDeclaration,
path: IElement<any>[],
) =>
ts.createTypeAliasDeclaration(
/* decorators */ undefined,
/* modifiers */ element.export === true
? [ts.createToken(ts.SyntaxKind.ExportKeyword)]
: undefined,
/* name */ element.name,
/* typeParameters */ create_type_parameters(element.generics, path),
/* type */ transform(
element.type || any_type,
path,
) as ts.TypeNode,
);