-1

I have a NodeJS project which I would like to have typescript files on it. My first attempt was to create a typescript file with content below.

utilts.ts :

export const delimitify = ( strings:Array<string>, delimiter:string ):string => strings.join(delimiter);
export const slugify = ( strings:Array<string> ):string => strings.join('_');
export const hyphenify = ( strings:Array<string> ):string => strings.join('-');
export const andify = ( strings:Array<string> ):string => strings.join('&');
export const orify = ( strings:Array<string> ):string => strings.join('|');

On the test file, I write the content below:

/__test__/utilts.test.js

import {
  delimitify
} from "../utilts";

describe(
  "delimitify",
  () => {
    it(
      "must return string delimited by hyphen", 
      () => {
        const result = delimitify(['1', '2', '3'], '-');
        
        expect(result).toBe('1-2-3')
      }
    );
  }
);

The compiler complains

SyntaxError: /home/brunolnetto/github/quivero/prego/utils/testing/utilts.ts: Unexpected token, expected "," (2:35)

      1 |
    > 2 | export const delimitify = ( strings:Array<string>, delimiter:string ):string => strings.join(delimiter);
        |                                    ^
      3 | export const slugify = ( strings:Array<string> ):string => strings.join('_');
      4 | export const hyphenify = ( strings:Array<string> ):string => strings.join('-');
      5 | export const andify = ( strings:Array<string> ):string => strings.join('&');

    > 1 | import {
        | ^
      2 |   delimitify
      3 | } from "../utilts";

It seems a very easy problem to solve. This may be something educational for the community. :-)

1 Answers1

1

your ts must be compiled to js to be ran by node.

to initialize ts: npm i typescript && npx tsc --init

to run your project: npx tsc && node ./build/index.js

HannesH
  • 932
  • 2
  • 12