Prologue
Buffer is part of the Node.js API . Since TypeScript does not know the classes from Node.js by default, you will need to install declaration files (type definitions) for Node.js.
If you see the following error, you will have to manually set the type definitions:
Error TS2304: Cannot find the name "Buffer".
Setting Type Definitions
You can set type definitions using the typings tool. I will show you how to do it:
Install the typings tool with npm:
npm install -g typings
Set the type definitions for Node.js from the DefinitelyTyped ( ~dt ) repository:
typings install dt~node --global --save
The Typing tool will create the following typings/globals/node directory and link it to typings/index.d.ts . There will also be a file called typings.json (due to the --save parameter) that references the definitions of allowed types:
{ "globalDependencies": { "node": "registry:dt/node#6.0.0+20160621231320" } }
Note. If you see the error "typings \ globals \ node \ index.d.ts (71,26): error TS1110: Type expected", then your Node.js definition is too large. The typing tool has problems with the latest type declarations . In this case, just check the version in the typings.json file. For me, node#6.0.0+20160621231320 worked, but node#6.0.0+20161212163245 not.
- Now you need to add
index.d.ts as a triple-slash directive to your code (which uses the Buffer class):
YourClass.ts
/// <reference path="../../typings/index.d.ts" /> export class YourClass { private static toString(encoded: string): string { return new Buffer(encoded, "base64").toString(); } }
UPDATE:
With the release of TypeScript 2.0, a new type determination system has been announced.
Now you can forget about the typings tool. All you have to do is run this command to set the TypeScript definitions for Node.js :
npm install --save @types/node
Also make sure you have the following entries in tsconfig.json :
{ "compilerOptions": { "moduleResolution": "node", ... }, "exclude": [ "node_modules", ... ] }
PS If you need type definitions for other classes (than in Node.js), you can find them here: http://microsoft.imtqy.com/TypeSearch/ p>