简体   繁体   English

如何使用打字稿文件导入/导出类型定义

[英]How to import/export a type definition with a typescript file

I am moving over to vue from angular and trying to implement a 'service' as a simple typescript class. 我正在从角度出发进行探讨,并试图将“服务”实现为简单的打字稿类。 I was wondering how I would go about this, currently I have: 我想知道如何解决这个问题,目前我有:

import axios from 'axios'
import keys from 'libs/keys/api-keys'

export default class Google {

    textSearch(query: string, radius = 5000) {
        let url = `https://maps.googleapis.com/maps/api/place/textsearch/json?radius=${radius}&query=${query}` +
            `&key=${keys.googleApiKey}`

        return axios.get(url)
    }
    getPhoto(photoReference: string, maxwidth = 1600) {
        let url = `https://maps.googleapis.com/maps/api/place/photo?maxwidth=${maxwidth}` +
            `&photoreference=${photoReference}&key=${keys.googleApiKey}`

        return axios.get(url)
    }
}

as my class. 作为我的课。 Then I try and import it into my vue component: 然后,我尝试将其导入到vue组件中:

import google from 'src/libs/location/google'
google.textSearch(params.location)

but I get the error: 但是我得到了错误:

Property 'textSearch' does not exist on type 'typeof Google'

so then I tried throwing a default interface before the class and still get the same error: 因此,我尝试在类之前抛出默认接口,但仍然收到相同的错误:

import axios from 'axios'
import keys from 'libs/keys/api-keys'

export default interface Google {
    textSearch(query: string, radius?: number): void
}

export default class Google {

    textSearch(query: string, radius = 5000) {
        let url = `https://maps.googleapis.com/maps/api/place/textsearch/json?radius=${radius}&query=${query}` +
            `&key=${keys.googleApiKey}`

        return axios.get(url)
    }
    getPhoto(photoReference: string, maxwidth = 1600) {
        let url = `https://maps.googleapis.com/maps/api/place/photo?maxwidth=${maxwidth}` +
            `&photoreference=${photoReference}&key=${keys.googleApiKey}`

        return axios.get(url)
    }
}

What is the correct way to do this? 正确的方法是什么? Does the type have to be in an external .d.ts file? 类型是否必须在外部.d.ts文件中? If so how will typescript infer the type on import. 如果是这样,打字稿将如何推断导入的类型。

textSearch is an instance method of the Google class. textSearchGoogle类的实例方法。 You are only importing the Google class and not an instance. 您仅导入Google类,而不是实例。 You need to create an instance to access textSearch method: 您需要创建一个实例来访问textSearch方法:

import Google from 'src/libs/location/google' // `Google` is the class here

let googleInstance = new Google();
googleInstance .textSearch(params.location);

Or if you wanted to export an instance of Google class you can do so: 或者,如果您要导出Google类的实例,则可以执行以下操作:

class Google {
    textSearch(query: string, radius = 5000) {
        // ...
    }
}

export default new Google();

// And use it like:
import google from 'src/libs/location/google' // `google` is the instance here
google.textSearch(params.location);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM