dsherret / dsherret/ts-morph

[Question] How to create a TypeLiteral ?

Open
#1,333 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

Hello,

I'm trying to create a TypeAliasDeclaration with a TypeLiteral for the type attribute but it receives only a string or a function as value.

This an example of what I want to create

```typescript
export type Options = {
port?: number;
host?: string;
};
```

This is what I have currently

```typescript
sourceFile.addTypeAlias({
type: '{ port?: number; host?: number; }',
name: 'Options',
isExported: true,
});
```

Is there another way to achieve that ?

![Screenshot 2022-09-28 at 22 09 23](https://user-images.githubusercontent.com/6919604/192880769-b45f81b1-bc1c-4d36-8006-4c5d4ddafab2.png)

By doing something like this

```typescript
sourceFile.addTypeAlias({
type: new TypeLiteral({
propertySignatures: [
new PropertySignature({ hasQuestionToken: true, type: SyntaxKind.NumberKeyword, name: 'port' }),
new PropertySignature({ hasQuestionToken: true, type: SyntaxKind.StringKeyword, name: 'host' }),
]
}),
name: 'Options',
isExported: true,
});
```

Thanks.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.