TypeScript and Google Publisher Tags

TypeScript is a strongly typed, object-oriented programming language that compiles to JavaScript. TypeScript is a superset of JavaScript, and supports all of JavaScript's features plus optional static typing.

Since TypeScript is a superset of JavaScript, all working JavaScript code is also TypeScript code. However, TypeScript tooling can help detect and prevent bugs that you might not notice in plain JavaScript.

Get started

DefinitelyTyped is an open source project that maintains a repository of type declaration files for many packages, including the Google Publisher Tag (GPT) library. You can install the GPT types with npm from the @types/google-publisher-tag package.

npm install --save-dev @types/google-publisher-tag

Once installed, you have access to all of the types exposed by the googletag object in your own code. You can also take advantage of code completion and content assist for GPT methods and properties in source code editors that have those features, for example, Visual Studio Code.


The following demo re-implements our Get started example in TypeScript, using the @types/google-publisher-tag package and Vite.