기존 순수 자바스크립트 기본 코드 베이스에서 작성한 코드를 typescript로 import 시에 경로를 못찾는 현상이 일어나서 방법을 찾아보았다.
**최근 업데이트 (2021년)
tsconfig.json 에 아래 옵션을 추가하면 기존 JS 파일들을 ES module 방식에 따라 사용가능하다
{
"compilerOptions": {
"allowJs": true,
"checkJs": false
}
}
기존 모듈 a.js파일 같은 폴더안에 작성하 main.ts 에서 import 해서 사용한다고 할때 아래와 같은 방법으로 작성 가능하다.
// a.js
function hey() {
console.log("hey")
}
export { hey }
main.ts
var moduleA = require('a경로');
moduleA.hey();
써드 파티 모듈들도 비슷한 방법들로 사용이 가능한데 더 typescript 답게 사용하는 방법들이 있다.
이방법은 다음번에 알아보도록 하겠다.
추가 참고:
http://stackoverflow.com/questions/12763684/how-to-require-jquery-via-amd-in-typescript
참고:
1. https://github.com/Microsoft/TypeScript/issues/2712
2. http://stackoverflow.com/questions/13013366/import-typescript-module-using-only-ambient-definition-for-use-in-amd
3. http://stackoverflow.com/questions/33525027/importing-js-file-into-typescript
4. http://stackoverflow.com/questions/27417107/how-use-an-external-non-typescript-library-from-typescript-without-d-ts
5. http://www.codebelt.com/typescript/typescript-amd-with-requirejs-tutorial/
'Front-End > Java Script' 카테고리의 다른 글
Webpack 유용한 플러그인 모음 (0) | 2016.10.30 |
---|---|
현재 브라우저의 UTC(UTC) 타임 구하기 (0) | 2016.10.05 |
자바스크립트 모듈 번들러 (1) - webpack (0) | 2016.07.25 |
ES6 문법 Cheat Sheet (0) | 2016.04.21 |
JavaScript 객체 (0) | 2014.11.06 |