Home > Mobile >  importing types from @types without corresponding library
importing types from @types without corresponding library

Time:12-30

Sorry if the question is unclear but I don't know how else to put it. I have a Google Ads Script project that I develop in Typescript. I use BigQuery library. As you know in Google Ads you don't need to import any libraries (like in Node.js) because they are already available in global scope. So I only need to import types from https://www.npmjs.com/package/@types/google-apps-script. It works in the way that it cancels any errors saying that BigQuery is not defined etc. But can I import and use any particular interfaces? For example I have a function that returns TableFieldSchema.

const bqQuerySchemaGenerator = (description: string, name: string, type: string) => {
    const nameFieldSchema : any = BigQuery.newTableFieldSchema();
    nameFieldSchema.description = description;
    nameFieldSchema.name = name;
    nameFieldSchema.type = type;
    return nameFieldSchema
}

I would like to define a type that would show me what this function reaturns. I know that normally if I was using a corresponding libarary I would import something like import {TableFieldSchema} from "google-apps-script". But as I mentioned I don't use any external library so instead I would imagine something like this

import type {TableFieldSchema} from "@types/google-apps-script"

const bqQuerySchemaGenerator = (description: string, name: string, type: string) : TableFieldSchema => {
    const nameFieldSchema : any = BigQuery.newTableFieldSchema();
    nameFieldSchema.description = description;
    nameFieldSchema.name = name;
    nameFieldSchema.type = type;
    return nameFieldSchema
}

but it doesn't work. How can I import these types? Or is it even possible?

CodePudding user response:

If you've installed the types package that you mentioned:

npm install --save-dev @types/google-apps-script

then you should be able to access the types (without an import statement) using the GoogleAppsScript namespace on the global object, just like you can access the values you showed (e.g. the BigQuery object):

const bqQuerySchemaGenerator = (description: string, name: string, type: string): GoogleAppsScript.BigQuery.Schema.TableFieldSchema => {
  const nameFieldSchema = BigQuery.newTableFieldSchema();
  nameFieldSchema.description = description;
  nameFieldSchema.name = name;
  nameFieldSchema.type = type;
  return nameFieldSchema;
};

Alternatively, you can also alias any type if you plan to reuse it. That way you won't have to type the full namespace path every time:

type TableFieldSchema = GoogleAppsScript.BigQuery.Schema.TableFieldSchema;

const bqQuerySchemaGenerator = (description: string, name: string, type: string): TableFieldSchema => {
  const nameFieldSchema = BigQuery.newTableFieldSchema();
  nameFieldSchema.description = description;
  nameFieldSchema.name = name;
  nameFieldSchema.type = type;
  return nameFieldSchema;
};

  • Related