Corey Alix
Corey Alix

Reputation: 2750

Can vscode generate type definitions?

I have been using a trick to get quick typings:

Derive typings from actual data

I derive my typings from actual data:

const ParcelFeatureTypeInstance = {"displayFieldName":"NAMECO","fieldAliases":{"PIN":"PIN / Tax Map #","OWNAM1":"Owner Name(MixedCase)",...};
type ParcelFeatureType = typeof ParcelFeatureTypeInstance;

But I would like to take it a step further and then copy the actual type definition from the popup window that opens when I hover over the type, but the definition is incomplete:

const ParcelFeatureTypeInstance: {
    displayFieldName: string;
    fieldAliases: {
        PIN: string;
        OWNAM1: string;
        OWNAM2: string;
        STREET: string;
        CITY: string;
        STATE: string;
        ZIP5: string;
        NAMECO: string;
        POWNNM: string;
        DEEDDATE: string;
        CUBOOK: string;
        ... 23 more ...;
        OBJECTID: string;
    };
    geometryType: string;
    spatialReference: {
        ...;
    };
    fields: ({
        ...;
    } | {
        ...;
    })[];
    features: {
        ...;
    }[];
}

Is there a way to get the full definition?

Upvotes: 2

Views: 936

Answers (1)

Emilio Platzer
Emilio Platzer

Reputation: 2469

You can declare and emit a .d.ts file.

Try this.

In example.ts file put your constants and types:

const ParcelFeatureTypeInstance = {"displayFieldName":"NAMECO","fieldAliases":{"PIN":"PIN / Tax Map #","OWNAM1":"Owner Name(MixedCase)"}};
export type ParcelFeatureType = typeof ParcelFeatureTypeInstance;

In tsconfig.json put at least:

{
  "compilerOptions": {
    "declaration": true
  }
}

Then, run the compiler:

tsc -p tsconfig.json

You obtain a new file example.d.ts with the type declarations of your constants an variables.

Upvotes: 3

Related Questions