@ssen/require-typescript
TypeScript icon, indicating that this package has built-in type declarations

4.0.0 • Public • Published

Require from Typescript source

// hello.ts
export const hello: number = 3;
// main
import { requireTypescript } from '@ssen/require-typescript';

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

Test Codes

__tests__/requireTypescript.test.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);
  });
});

Readme

Keywords

none

Package Sidebar

Install

npm i @ssen/require-typescript

Weekly Downloads

4

Version

4.0.0

License

MIT

Unpacked Size

18 kB

Total Files

5

Last publish

Collaborators

  • ssen