0

원숭이가 Date 클래스에 패치하는 cached-date이라는 NPM 패키지를 작성했습니다. 표준 문자열 표현을 캐시합니다.기본 제공 노드 클래스에 대한 외부 원숭이 패치를 선언합니다

// Typescript application 

require('cached-date'); 
const date = new Date(); 
const isoStr = date.toCachedISOString(); 
:

// index.d.ts 

declare interface Date { 
    toCachedString(): string; 
    toCachedDateString(): string; 
    toCachedISOString(): string; 
    toCachedJSON(): string; 
    toCachedTimeString(): string; 
    toCachedUTCString(): string; 
} 

다음 코드는 컴파일러 오류 Property 'toCachedISOString' does not exist on type 'Date'을 산출 :

모든 것은 내가 유형의 정의가 패키지에 포함 된 인식 내 타이프 라이터 프로젝트를 얻을 수없는 것을 제외하고, 좋은 작품 다음

package.json의 중요한 부분이다

// package.json 

"main": "index.js", 
"types": "index.d.ts", 
,745,

궁금한 점은 index.d.ts을 내 프로젝트의 로컬 선언 폴더 ("paths" : tsconfig.json)로 이동하고 변경하지 않고 모두 Date.d.ts이라고 할 때 모든 것이 잘 수행됩니다.

// application 

export interface Date { 
    toCachedString(): string; 
    toCachedDateString(): string; 
    toCachedISOString(): string; 
    toCachedJSON(): string; 
    toCachedTimeString(): string; 
    toCachedUTCString(): string; 
} 

나 외부 내장의 유형 선언을 병합하기위한 특별한 방법이 있나요 : 나는 응용 프로그램에서 다음과 같은 선언을 넣을 때

마찬가지로, 모두가 잘? 감사!

답변

0

나는 그것을 알아 냈다. 실제로 조금 당황 스럽습니다. 그냥이 대체 :이와

require("cached-date"); 

을 :

import "cached-date"; 

이제 타이프는 모듈의 선언에서 끌어와 모두 잘 있습니다.

이것은 선언없이 모듈을로드하는 방법을 제안합니다.