1

Type-Script가 아닌 모듈을 TypeScript 프로젝트로 가져 오려고합니다.Typescript import @ google-cloud/pubsub

이 프로젝트에는 자체 선언이나 @types 선언이 없으므로 모듈에 대한 자체 선언을 만들었습니다. 내가 선언 파일에 모듈을 선언 할 때, 나는 다음과 같은 오류 얻을 :

import stream from 'stream' 
import events from 'events' 

interface ConfigurationObject extends Object { 
    projectId?: string 
    keyFilename?: string 
    email?: string 
    credentials?: CredentialsObject 
    autoRetry?: boolean 
    maxRetries?: number 
    promise?: Function 
} 

interface CredentialsObject extends Object { 
    client_email?: string 
    private_key?: string 
} 

interface QueryOptions extends Object { 
    autoPaginate?: boolean 
    maxApiCalls?: number 
    maxResults?: number 
    pageSize?: number 
    pageToken?: string 
} 

interface SnapshotQueryOptions extends QueryOptions { } 

interface TopicsQueryOptions extends Object { } 

interface SubscriptionQueryOptions extends Object { 
    topic?: string 
} 

interface SubscribeOptions extends Object { 
    ackDeadlineSeconds: number 
    autoAck: boolean 
    encoding: string 
    interval: number 
    maxInProgress: number 
    pushEndpoint: string 
    timeout: number 
} 

interface SubscriptionOptions extends Object { 
    autoAck?: boolean 
    encoding?: string 
    interval?: number 
    maxInProgress?: number 
    timeout?: number 
} 

interface SubscriptionObject extends Object { 
    name: string 
    topic: string 
    pushConfig: PushConfigObject 
    ackDeadlineSeconds: number 
} 

interface PushConfigObject extends Object { 
    pushEndpoint: string 
    attributes: { 
     [key: string]: string 
    } 
} 

interface TopicObject extends Object { 
    name: string 
} 

interface SnapshotObject extends Object { 
    name: string 
} 

interface Message { 
    id: string 
    ackId: string 
    data: any 
    attributes: any 
    timestamp: number 

    ack(callback: Function): void 
    skip(): void 
} 

declare type ApiCallbackFunction<T> = (err: Error | null, data: T, apiResponse: any) => void 

declare type CallbackFunction<T> = (err: Error | null, data: T) => void 

declare type ApiPromiseResult<T> = [T, any] 

declare class Subscription extends events.EventEmitter { 
    ack(
     ackIds: string | string[], 
     options?: { 
      timeout: number 
     }, 
     callback?:() => void 
    ): Promise<void> | void 

    create(
     options?: SubscribeOptions, 
     callback?: ApiCallbackFunction<SubscriptionObject> 
    ): Promise<ApiPromiseResult<SubscriptionObject>> | void 

    createSnapshot(
     name: string, 
     callback?: ApiCallbackFunction<SnapshotObject> 
    ): Promise<ApiPromiseResult<SnapshotObject>> | void 
} 

declare class PubSub { 
    constructor(
     config: ConfigurationObject 
    ) 

    createTopic(
     name: string, 
     callback?: ApiCallbackFunction<TopicObject> 
    ): Promise<ApiPromiseResult<TopicObject>> | void 

    getSnapshots(
     options?: SnapshotQueryOptions, 
     callback?: CallbackFunction<SnapshotObject[]> 
    ): Promise<any[]> | void 

    getSnapshotsStream(
     options?: SnapshotQueryOptions 
    ): stream.Readable 

    getSubscriptions(
     options?: SubscriptionQueryOptions, 
     callback?: ApiCallbackFunction<SubscriptionObject[]> 
    ): Promise<ApiPromiseResult<SubscriptionObject[]>> | void 

    getSubscriptionsStream(
     options?: SubscriptionQueryOptions 
    ): stream.Readable 

    getTopics(
     options?: TopicsQueryOptions, 
     callback?: ApiCallbackFunction<TopicObject[]> 
    ): Promise<ApiPromiseResult<TopicObject[]>> | void 

    getTopicsStream(
     options?: TopicsQueryOptions 
    ): stream.Readable 

    snapshot(
     name: string 
    ): any 

    subscribe(
     topic: TopicObject | string, 
     subName?: stream, 
     options?: SubscribeOptions, 
     callback?: ApiCallbackFunction<SubscriptionObject> 
    ): Promise<ApiPromiseResult<SubscriptionObject>> | void 

    subscription(
     name?: string, 
     options?: SubscriptionOptions 
    ): void 

    topic(
     name: string 
    ): TopicObject 
} 

declare module '@google-cloud/pubsub' { 
    export = PubSub 
} 

답변

0

:

Invalid module name in augmentation. Module '@google-cloud/pubsub' resolves to an untyped module at './node_modules/@google-cloud/pubsub/src/index.js', which cannot be augmented.

내가 여기 타이프 라이터 2.2.2

을 사용하고 있습니다를 전체 선언 파일입니다 다른 형식이없는 모듈에 대한 정의를 작성하려고 할 때 같은 문제가 발생했습니다. 내가 발견 한 것은 형식이 지정되지 않은 모듈에 대한 정의를 작성하는 경우 declare module이 전체 정의 파일을 포함하는지 확인해야한다는 것입니다.

예를 들어 간단한 테스트 프로젝트를 작성하고 @google-cloud/pubsub 모듈을 가져 오면 다음 정의 파일이 컴파일됩니다. 불행히도 나는 왜 이것이 작동하는지 설명하는 문서를 찾지 못했습니다.

index.d.ts

declare module '@google-cloud/pubsub' { 
    namespace pubsub { 
    class PubSub { 
     topic (name: string) : Topic; 
    } 
    class Topic { 
     subscribe (subscriptionName: string, options: Object, callback: Function): void; 
    } 
    } 
    function pubsub(options: any): pubsub.PubSub; 
    export = pubsub; 
} 

subscribe.ts :

declare module '@google-cloud/pubsub' { 
    import * as stream from 'stream'; 
    import * as events from 'events'; 

    interface ConfigurationObject extends Object { 
     projectId?: string 
     keyFilename?: string 
     email?: string 
     credentials?: CredentialsObject 
     autoRetry?: boolean 
     maxRetries?: number 
     promise?: Function 
    } 

    interface CredentialsObject extends Object { 
     client_email?: string 
     private_key?: string 
    } 

    interface QueryOptions extends Object { 
     autoPaginate?: boolean 
     maxApiCalls?: number 
     maxResults?: number 
     pageSize?: number 
     pageToken?: string 
    } 

    interface SnapshotQueryOptions extends QueryOptions { } 

    interface TopicsQueryOptions extends Object { } 

    interface SubscriptionQueryOptions extends Object { 
     topic?: string 
    } 

    interface SubscribeOptions extends Object { 
     ackDeadlineSeconds: number 
     autoAck: boolean 
     encoding: string 
     interval: number 
     maxInProgress: number 
     pushEndpoint: string 
     timeout: number 
    } 

    interface SubscriptionOptions extends Object { 
     autoAck?: boolean 
     encoding?: string 
     interval?: number 
     maxInProgress?: number 
     timeout?: number 
    } 

    interface SubscriptionObject extends Object { 
     name: string 
     topic: string 
     pushConfig: PushConfigObject 
     ackDeadlineSeconds: number 
    } 

    interface PushConfigObject extends Object { 
     pushEndpoint: string 
     attributes: { 
      [key: string]: string 
     } 
    } 

    interface TopicObject extends Object { 
     name: string 
    } 

    interface SnapshotObject extends Object { 
     name: string 
    } 

    interface Message { 
     id: string 
     ackId: string 
     data: any 
     attributes: any 
     timestamp: number 

     ack(callback: Function): void 
     skip(): void 
    } 

    export type ApiCallbackFunction<T> = (err: Error | null, data: T, apiResponse: any) => void 

    export type CallbackFunction<T> = (err: Error | null, data: T) => void 

    export type ApiPromiseResult<T> = [T, any] 

    export class Subscription extends events.EventEmitter { 
     ack(
      ackIds: string | string[], 
      options?: { 
       timeout: number 
      }, 
      callback?:() => void 
    ): Promise<void> | void 

     create(
      options?: SubscribeOptions, 
      callback?: ApiCallbackFunction<SubscriptionObject> 
    ): Promise<ApiPromiseResult<SubscriptionObject>> | void 

     createSnapshot(
      name: string, 
      callback?: ApiCallbackFunction<SnapshotObject> 
    ): Promise<ApiPromiseResult<SnapshotObject>> | void 
    } 

    export class PubSub { 
     constructor(
      config: ConfigurationObject 
    ) 

     createTopic(
      name: string, 
      callback?: ApiCallbackFunction<TopicObject> 
    ): Promise<ApiPromiseResult<TopicObject>> | void 

     getSnapshots(
      options?: SnapshotQueryOptions, 
      callback?: CallbackFunction<SnapshotObject[]> 
    ): Promise<any[]> | void 

     getSnapshotsStream(
      options?: SnapshotQueryOptions 
    ): stream.Readable 

     getSubscriptions(
      options?: SubscriptionQueryOptions, 
      callback?: ApiCallbackFunction<SubscriptionObject[]> 
    ): Promise<ApiPromiseResult<SubscriptionObject[]>> | void 

     getSubscriptionsStream(
      options?: SubscriptionQueryOptions 
    ): stream.Readable 

     getTopics(
      options?: TopicsQueryOptions, 
      callback?: ApiCallbackFunction<TopicObject[]> 
    ): Promise<ApiPromiseResult<TopicObject[]>> | void 

     getTopicsStream(
      options?: TopicsQueryOptions 
    ): stream.Readable 

     snapshot(
      name: string 
    ): any 

     subscribe(
      topic: TopicObject | string, 
      subName?: stream, 
      options?: SubscribeOptions, 
      callback?: ApiCallbackFunction<SubscriptionObject> 
    ): Promise<ApiPromiseResult<SubscriptionObject>> | void 

     subscription(
      name?: string, 
      options?: SubscriptionOptions 
    ): void 

     topic(
      name: string 
    ): TopicObject 
    } 
} 

/펍과 놀아 조금 Sub이 후 나는 다음과 같은 개념 증명 코드를 내놓았다

import * as pubsub from '@google-cloud/pubsub'; 

let ps = pubsub({ 
    projectId: 'project-id', 
    keyFilename: 'key.json' 
}); 

console.log('Subscribed to pubsub...'); 

ps.topic('test').subscribe('test', {autoAck: true}, (err: any, subscription: any) => { 
    if (err) { 
    console.log(err); 
    } else { 
    subscription.on('error', (err: any) => { 
     console.log(err); 
    }); 
    subscription.on('message', (message: any) => { 
     console.log(message); 
    }); 
    } 
}); 
+0

고마워요! 다른 가져 오기 구조를 사용하여 "@ google-cloud/pubsub"패키지를 가져와야하는 유일한 차이점 만 설명하면 효과적입니다. 이제 다음과 같이 보입니다 :'import PubSub = require ('@ google-cloud/pubsub')'. – Siggy