benjamn / benjamn/recast

TypeScript types not exported correctly

Open
#587 4 comments 1 reaction 1 assignee Claimed by @benjamn View on GitHub
Dominant language
TypeScript
Stars
5.3k
Forks
363
Avg merge
3d 8h
Merged PRs (30d)
3

Description

Code works, but `recast` is type `any` instead of `Main`:

```js
const recast = require( 'recast' )

const ast = recast.parse( 'const foo = 1' )

console.log( recast.print( ast ).code )
```

But it gets worse when using it from TypeScript using module syntax:

```ts
import * as recast from 'recast'

const { parse, print } = recast

const ast = parse( 'const foo = 1' )

console.log( print( ast ).code )
```

`Type 'typeof import("xxx/node_modules/recast/main")' has no property 'parse' and no string index signature.`

This makes TypeScript happy, but it fails at runtime:

```ts
import { default as recast } from 'recast'

const { parse, print } = recast

const ast = parse( 'const foo = 1' )

console.log( print( ast ).code )
```

`Cannot destructure property `parse` of 'undefined' or 'null'`

However it does get the correct function signatures for `parse` and `print` in this case, eg `const parse: (source: string, options?: Partial | undefined) => any`

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.