I am having a hard time getting typescript to recognize the shape of a local es6 module:
convert-time.js
export const minutes = sec => sec * 60
export const hours = sec => minutes(sec) * 60
export const days = sec => hours(sec) * 24
export const weeks = sec => days(sec) * 24
export const years = sec => days(sec) * 365
Note: I would like to figure out how to get this to work without just changing convert-time.js to a typescript file.
Here is the file I am trying to import it into:
index.ts
/// <reference path="../typings/convert-time.d.ts" />
import { minutes, days } from '../test/helpers/convert-time'
Here is the type definition file I created:
convert-time.d.ts:
declare module "convert-time" {
export function minutes(sec: number): number;
export function hours(sec: number): number;
export function days(sec: number): number;
export function weeks(sec: number): number;
export function years(sec: number): number;
}
And here is my tsconfig:
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": false,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"jsx": "react"
},
"files": [
"typings/index.d.ts",
"typings/convert-time.d.ts"
],
"exclude": [
"node_modules"
]
}
Steps tried:
- Searched StackOverflow for
error TS2307: Cannot find module. Most questions are regarding external modules which have type definitions. I couldn't find an example of a local module. - Read https://www.typescriptlang.org/docs/handbook/modules.html
- Read https://www.typescriptlang.org/docs/handbook/module-resolution.html
- Added
typings/convert-time.d.tsto thefilessection intsconfig.json. - Added
/// <reference path='../../typings/convert-time.d.ts' />toindex.ts
None of the above worked! What dark magic must I learn to get this to work? Thanks!
compilerOptionsyou need to haveallowJs: trueto allow for JavaScript files otherwise it will thow an error.