Skip to content

Add TypeScript support #1170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ As of version 3.0.0, the SDK has been updated to use modern JavaScript features

## TypeScript Support

- Full TypeScript support for mixed JS/TS codebases
- Type definitions in index.d.ts
- TypeScript compilation via babel-preset-typescript
- See examples/typescript for a complete example
- For new development, both .js and .ts files are supported
- Add JSDoc types to enable intellisense when needed

## Common Patterns
Expand Down
5 changes: 5 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ var fs = require('fs');

var webpackConfig = require('./webpack.config.js');

require('ts-node').register({
transpileOnly: true,
compilerOptions: { module: 'commonjs' },
});

function findTests(context) {
if (context !== 'browser') {
return {};
Expand Down
6 changes: 2 additions & 4 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"parserOpts": {
"ecmaVersion": 2021,
"sourceType": "module",
"sourceType": "module"
},
"presets": [
"@babel/preset-env"
]
"presets": ["@babel/preset-env", "@babel/preset-typescript"]
}
36 changes: 36 additions & 0 deletions examples/typescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# TypeScript Example

This example shows how to use rollbar.js with TypeScript. The example demonstrates how to:

- Import Rollbar in a TypeScript file
- Configure a Rollbar instance
- Use TypeScript interfaces with Rollbar
- Log messages and data with strong typing

## Requirements

- Node.js v18+
- npm or yarn

## Running the example

```bash
# Install dependencies
npm install

# Build the TypeScript files
npm run build

# Run the example
npm start
```

## How it works

The project includes:

1. TypeScript configuration in `tsconfig.json`
2. Dependencies for TypeScript in `package.json`
3. Example TypeScript code in `src/example.ts`

This example works because the rollbar.js library includes TypeScript definitions in `index.d.ts` and the build system has been configured to support TypeScript files.
21 changes: 21 additions & 0 deletions examples/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "typescript-example",
"version": "1.0.0",
"description": "Example of using Rollbar with TypeScript",
"main": "dist/example.js",
"scripts": {
"tsc": "tsc",
"build": "tsc",
"start": "node ./dist/example.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"@types/node": "^18.0.0",
"rollbar": "file:../../"
}
}
30 changes: 30 additions & 0 deletions examples/typescript/src/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as Rollbar from 'rollbar';

const rollbar = new Rollbar({
accessToken: 'POST_SERVER_ITEM_ACCESS_TOKEN',
captureUncaught: true,
captureUnhandledRejections: true
});

// TypeScript example using strongly-typed parameters
interface User {
id: number;
name: string;
email: string;
}

function processUser(user: User): void {
rollbar.info(`Processing user: ${user.name}`, user);
}

// Create a user and process it
const user: User = {
id: 123,
name: 'Example User',
email: 'user@example.com'
};

processUser(user);

// Log a message
rollbar.info('Hello from TypeScript!');
12 changes: 12 additions & 0 deletions examples/typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export = function error() {

class CustomError extends Error {
constructor(message: string) {
super(`Lorem "${message}" ipsum dolor.`);
this.name = 'CustomError';
}
}
// TypeScript code snippet will include `<Error>`
var error = <Error> new CustomError('foo');
throw error;
}
12 changes: 12 additions & 0 deletions examples/typescript/src/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export = function error() {

class CustomError extends Error {
constructor(message: string) {
super(`Lorem "${message}" ipsum dolor.`);
this.name = 'CustomError';
}
}
// TypeScript code snippet will include `<Error>`
var error = <Error> new CustomError('foo');
throw error;
}
10 changes: 10 additions & 0 deletions examples/typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "dist",
"sourceMap": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}
Loading
Loading