# @ssen/require-typescript

> ```ts // hello.ts export const hello: number = 3; ```

Latest version **4.0.0** (published 2022-06-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ssen/require-typescript
pnpm add @ssen/require-typescript
yarn add @ssen/require-typescript
bun add @ssen/require-typescript
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2022-06-02 |
| First published | 2020-05-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14 |
| Dependencies | 0 |
| Unpacked size | 17.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | SSen |
| Maintainers | ssen |

## Links

- npm: https://www.npmjs.com/package/@ssen/require-typescript
- Repository: https://github.com/rocket-hangar/rocket-punch
- Homepage: https://github.com/rocket-hangar/rocket-punch/tree/master/packages/src/@ssen/require-typescript
- Issues: https://github.com/rocket-hangar/rocket-punch/issues
- npm.io page: https://npm.io/package/@ssen/require-typescript

## Recent versions

- 4.0.0 (latest) — 2022-06-02
- 4.0.0-alpha.1 (canary) — 2022-05-27
- 3.0.3 — 2021-12-07
- 3.0.2 — 2021-11-01
- 3.0.2-alpha.1 — 2021-11-01
- 3.0.0 — 2021-06-17
- 3.0.0-alpha.7 — 2021-06-13
- 3.0.0-alpha.5 — 2021-06-13
- 3.0.0-alpha.3 — 2021-06-13
- 3.0.0-alpha.1 — 2021-06-13
- 2.5.1 — 2021-06-02
- 2.5.0 — 2021-04-03
- 2.4.1 — 2021-02-10
- 2.4.0 — 2021-01-31
- 2.3.2 — 2020-11-11
- … 39 more at https://npm.io/package/@ssen/require-typescript/versions

## README

# Require from Typescript source

```ts
// hello.ts
export const hello: number = 3;
```

```js
// main
import { requireTypescript } from '@ssen/require-typescript';

const { hello } = requireTypescript < { hello: number } > './hello.ts';
console.assert(hello === 3);
```

# Test Codes

<!-- source __tests__/*.test.ts -->

[\_\_tests\_\_/requireTypescript.test.ts](__tests__/requireTypescript.test.ts)

```ts
import path from 'path';
import { requireTypescript } from '@ssen/require-typescript';
import process from 'process';
import { describe, test, expect } from 'vitest';

describe('requireTypescript', () => {
  test.each(['basic', 'js'])('should get exports from %s', (dir: string) => {
    const { hello } = requireTypescript<{ hello: number }>(
      path.join(process.cwd(), `test/fixtures/require-typescript/${dir}/hello`),
    );
    expect(hello).toBe(1);

    const { hello2 } = requireTypescript<{ hello2: number }>(
      path.join(
        process.cwd(),
        `test/fixtures/require-typescript/${dir}/with-import`,
      ),
    );
    expect(hello2).toBe(2);

    const { hello3 } = requireTypescript<{ hello3: number }>(
      path.join(
        process.cwd(),
        `test/fixtures/require-typescript/${dir}/with-module`,
      ),
    );
    expect(hello3).toBe(1);

    const { default: func } = requireTypescript<{
      default: (a: number, b: number) => number;
    }>(
      path.join(process.cwd(), `test/fixtures/require-typescript/${dir}/func`),
    );
    expect(func(1, 2)).toBe(3);

    const { some } = requireTypescript<{ some: string }>(
      path.join(
        process.cwd(),
        `test/fixtures/require-typescript/${dir}/dirname`,
      ),
    );
    expect(some).toBe(
      path.join(
        path.join(
          process.cwd(),
          `test/fixtures/require-typescript/${dir}/hello`,
        ),
      ),
    );
  });

  test('should get exports with index file', () => {
    const { hello } = requireTypescript<{ hello: number }>(
      path.join(process.cwd(), 'test/fixtures/require-typescript/index/hello'),
    );
    expect(hello).toBe(1);
  });

  test('should get typescript exports', () => {
    const { hello } = requireTypescript<{ hello: number }>(
      path.join(
        process.cwd(),
        'test/fixtures/require-typescript/basic/hello.ts',
      ),
    );
    expect(hello).toBe(1);

    const { hello2 } = requireTypescript<{ hello2: number }>(
      path.join(
        process.cwd(),
        'test/fixtures/require-typescript/basic/with-import.ts',
      ),
    );
    expect(hello2).toBe(2);

    const { hello3 } = requireTypescript<{ hello3: number }>(
      path.join(
        process.cwd(),
        'test/fixtures/require-typescript/basic/with-module.ts',
      ),
    );
    expect(hello3).toBe(1);

    const { default: func } = requireTypescript<{
      default: (a: number, b: number) => number;
    }>(
      path.join(
        process.cwd(),
        'test/fixtures/require-typescript/basic/func.ts',
      ),
    );
    expect(func(1, 2)).toBe(3);
  });

  test('should get javascript exports', () => {
    const { hello } = requireTypescript<{ hello: number }>(
      path.join(process.cwd(), 'test/fixtures/require-typescript/js/hello.js'),
    );
    expect(hello).toBe(1);

    const { hello2 } = requireTypescript<{ hello2: number }>(
      path.join(
        process.cwd(),
        'test/fixtures/require-typescript/js/with-import.js',
      ),
    );
    expect(hello2).toBe(2);

    const { hello3 } = requireTypescript<{ hello3: number }>(
      path.join(
        process.cwd(),
        'test/fixtures/require-typescript/js/with-module.js',
      ),
    );
    expect(hello3).toBe(1);

    const { default: func } = requireTypescript<{
      default: (a: number, b: number) => number;
    }>(path.join(process.cwd(), 'test/fixtures/require-typescript/js/func.js'));
    expect(func(1, 2)).toBe(3);
  });
});
```

<!-- /source -->

---
_Source: https://npm.io/package/@ssen/require-typescript · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
