EzDev.org

DefinitelyTyped

DefinitelyTyped - The repository for high quality TypeScript type definitions. Home | DefinitelyTyped the repository for high quality typescript type definitions


typescript getting error TS2304: cannot find name ' require'

I am trying to get my first Typescript and DefinitelyTyped node app up and running, and running into some errors.

I am getting the error "TS2304: Cannot find name 'require' " when I attempt to transpile a simple ts node page. I have read through several other occurrences of this error on SO and I do not think I have similar issues.
I am running at the shell prompt the command : tsc movie.server.model.ts. The contents of this file are:

'use strict';

/// <reference path="typings/tsd.d.ts" />

/*  movie.server.model.ts - definition of movie schema */

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var foo = 'test';

The error is thrown on the var mongoose=require('mongoose') line

The contents of the typings/tsd.d.ts file are:

/// <reference path="node/node.d.ts" />
/// <reference path="requirejs/require.d.ts" />

The .d.ts file references were placed in the appropriate folders and added to typings/tsd.d.ts by the commands:

tsd install node --save
tsd install require --save

The produced .js file seems to work fine, so I could ignore the error. But I would appreciate knowing why this error occurs and what I am doing wrong.


Source: (StackOverflow)

How do I define a knockout binding handler in typescript?

I normally add custom knockout binding handlers in JavaScript via

ko.bindingHandlers.myBindingHandler = {...}

but now I have to add them in TypeScript via

ko.bindingHandlers["myBindingHandler"] = {...}

otherwise I get this error because I'm using typescript.d.ts:

The property 'myBindingHandler' does not exist on value of type 'KnockoutBindingHandlers'

I don't like the ["property"] approach because then I can't reference it or get intellisense on it later.

So, how can I add my custom binding handler to knockout while using definitelyTyped's knockout definition, while also being able to reference my definition via intellisense, etc?


Source: (StackOverflow)

100+ errors in jQuery.d.ts since getting latest via nuget in MVC application

Ever since I updated to the latest jQuery.d.ts definition file, using the Visual Studio NuGet package manager, I now get 100s of errors within the jQuery.d.ts file.

The pattern to all the errors is vertical lines like this:

index(selector: string|JQuery|Element): number;

which I assume indicates optional types. The errors mostly look like:

\Scripts\typings\jquery\jquery.d.ts(2797,34): error TS1005: Build: ',' expected.

The default Build Action was TypeScriptCompile, but changing that to none has no effect.

I am running Visual studio 2013 Professional release 4, so assumed I would have the latest TypeScript version, but this looks like a versioning issue.

Any ideas on how to resolve the problem.


Source: (StackOverflow)

Why is TSD deprecated?

Official tsd annouced that tsd is deprecated and recommend using typings instead. Why is TSD deprecated? What is the difference between tsd and typings. What is the advantage of typings?


Source: (StackOverflow)

How do I add a TypeScript definitely typed definition in ASP.NET 5?

I'm trying to install TypeScript definition files from DefinitelyTyped within a ASP.NET v5 web project in Visual Studio 2015 RC? NuGet packages don't seem to work anymore and TSD hasn't been updated in awhile and I haven't seen a lot of good documentation for it when using it within VS.

What ways are still working for the VS 2015 RC?


Source: (StackOverflow)

angularjs definitely typed visual studio 2015

I'm trying to get my first angularjs project to work within Visual Studio 2015. Worked fine until I tried to move to Typescript. Installed the DefinitelyTyped projects and get lots of error messages.

Here is how to reproduce it:

  • Create an empty ASP.NET 4.5.2 application
  • Nuget AngularJS Core 1.4.4
  • Nuget AngularJS.TypeScript.DefinitelyTyped 4.1.1 (includes jQuery 0.0.1)
  • Add an empty TypeScript file
  • Add

    /// <reference path="typings/angularjs/angular.d.ts" />
    

    to that file.

  • that's when I get 444 errors, starting with

    Error   TS2411  Property 'find' of type '{ (selector: string): 
    IAugmentedJQuery; (element: any): IAugmentedJQuery; (obj: JQuery): 
    IAugment...' is not assignable to string index type 'HTMLElement'.  
    TypeScript Virtual Projects l:\M\eigene dateien\visual studio 
    2015\Projects\WebApplication3\WebApplication3\Scripts\typings
    \angularjs\angular.d.ts
    

Any idea what I'm doing wrong or how to work around it?


Source: (StackOverflow)

How to install express in typings?

I am trying to use expressjs in my app.

After installing it using typings install express --ambient --save, I run tsc, but I get two errors:

typings/main/ambient/express/index.d.ts(17,34): error TS2307: Cannot find module 'serve-static'. typings/main/ambient/express/index.d.ts(18,27): error TS2307: Cannot find module 'express-serve-static-core'.

So, I tried to install both:

typings install serve-static --ambient --save
typings install express-serve-static --ambient --save

and then I run tsc again, but get one more error:

typings/main/ambient/serve-static/index.d.ts(79,24): error TS2307: Cannot find module 'mime'.

How can I solve these problems? How can I install all dependencies of express automatically?


Source: (StackOverflow)

Extend interface defined in .d.ts file

In my TypeScript project, I use DefinitelyTyped definitions for external js dependencies.

Sometimes it might happen that these definitions are outdated. It might also happen than some libraries can add new methods at runtime, like express-validator in which you can define custom validator functions.

Therefore I would like to extend those .d.ts definitions adding new methods and/or properties.

So if I have my DefinitelyTyped defininiton in express-validator.d.ts:

declare module ExpressValidator {
  export interface Validator {
    is(): Validator;
    not(): Validator;
    isEmail(): Validator;
    ...
  }
}

how can I extend Validator interface within, for example, my application.ts ?

///<reference path='../typings/tsd.d.ts' />

import expressValidator = require('express-validator');
export var app = express();

app.use(expressValidator({
    customValidators: {
        isArray: function(value) {
            return Array.isArray(value);
        }
 }
}));

// How to extend Validator interface adding isArray() method??

Source: (StackOverflow)

typescript: What to do if a typings (or tsd) is not available?

I was looking over the typescript handbook and I can't seem to find the answer.

if I am using a library that no typings exist what are my options.

One is to create the typings file, but this I don't really want to do.

What are my other options, I seem to remember some kind of 'declare' keyword ?

Or maybe something in the tsconfig ?

I assume there is a way of declaring this variable (type) globally so every time i use it, it would just work.

And I presume there is a way of just declaring it only available in one file.

I am still very confused about this.

Of course the best way would be to include a typings file but this won't always be available.

Any help really appreciated

Thanks


Source: (StackOverflow)

How do I declare a Generic Promise in Typescript, so that when the Generic type is "", one of its methods won't take a parameter?

In Typescript, I want to be able to define a Promise's type in such a way so that I can do this:

//This works today:
new Promise<number>((resolve)=>{
   //Cool
   resolve(5);
   //Error, because I didn't pass a number:
   resolve();
}

//This is what I want to do also:
new Promise<void>((resolve)=>{
   //Error, because I passed a value:
   resolve(5);
   //Cool, because I declared the promise to be of type void, so resolve doesn't take a value:
   resolve();
}

The promise definition files I've seen all declare that the "resolve" method of a promise must take a value. Here is a recent example from the wonderful DefinitelyTyped project:

declare class Promise<R> implements Thenable<R> {
    constructor(callback: (resolve : (result: R) => void, reject: (error: any) => void) => void); 
///...
}

```

That basically says, "The resolve callback must be passed a value of type R." That's fine for a promise like new Promise<number>. Typescript will verify we're calling resolve with a value of type number.

However, what if I want a promise that doesn't have a value, so I want to be able to call resolve() without passing a value? I can declare my promise like this: new Promise<void> But then I'm still forced to call resolve and pass in a value of some sort. I can call resolve(undefined), but that reads a bit strangely.

There appears to be no way to properly capture this concept in Typescript: "If this generic has a type 'void', then don't expect a parameter for this function."

The closest I can do is mark the result as optional in the resolve method, but that would mean that the result is always optional, even for typed versions of Promises.


Source: (StackOverflow)

Why isn't Webpack excluding a folder I specified?

I keep my server & client code in the same repository, though I use Webpack to build only the client:

enter image description here

My project builds fine if I delete the src/server folder. But when it's there I get all these Webpack Typescript duplicate definition errors like:

[1m[31mERROR in /home/rje/projects/ekaya/typings/main/ambient/react-dom/index.d.ts
(70,5): error TS2300: Duplicate identifier 'export='.

which are caused by Webpack trying to build one of the files in my server folder that contains:

/// <reference path="../../../../typings/main.d.ts" />

How can I get Webpack to completely ignore the server folder?

I've tried in my webpack.config.js:

var rootPath = __dirname; 
var srcPath = path.join(rootPath, 'src/client');
var distPath = path.join(rootPath, 'dist/client');
var serverPath = path.join(rootPath, 'src/serve
...
loaders:
        [
            {test: /\.js$/, loader: 'babel-loader?cacheDirectory', include: [srcPath], exclude: [serverPath]},
            {test: /\.jsx$/, loader: 'babel-loader?cacheDirectory', include: [srcPath], exclude: [serverPath] },
            {test: /\.ts$/, loader: 'ts-loader?cacheDirectory', include: [srcPath], exclude: [serverPath] },
            {test: /\.tsx$/, loader: 'ts-loader?cacheDirectory', include: [srcPath], exclude: [serverPath] },

Here's the full webpack config if it helps:

//https://webpack.github.io/docs/configuration.html

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var path = require('path');
var rootPath = __dirname; // e.g.  ~/projects/ekaya
var srcPath = path.join(rootPath, 'src/client');
var distPath = path.join(rootPath, 'dist/client');
var serverPath = path.join(rootPath, 'src/server');

module.exports =
{
    bail: true,
    cache: false,
    context: rootPath,
    debug: true,
    devtool: 'source-map', //inline-source-map, https://webpack.github.io/docs/configuration.html#devtool
    target: 'web', //node, web
    devServer:
    {
        contentBase: distPath,
        historyApiFallback: true,
        outputPath: path.join(distPath, 'devServer')
    },
    entry:
    {
        app: path.join(srcPath, 'app/home.jsx'),
        lib: ['react', 'react-router', 'react-dom', 'jquery', 'lodash', 'history']
    },
    output:
    {
        path: distPath,
        publicPath: '',
        filename: '[name].js',
        pathInfo: true
    },
    resolve:
    {
        root: srcPath,
        extensions: ['', '.js', '.jsx', '.ts', '.tsx'],
        modulesDirectories: ['node_modules', srcPath, 'typings']
    },
    module:
    {
        loaders:
        [
            {test: /\.js$/, loader: 'babel-loader?cacheDirectory', include: [srcPath], exclude: [serverPath]},
            {test: /\.jsx$/, loader: 'babel-loader?cacheDirectory', include: [srcPath], exclude: [serverPath] },
            {test: /\.ts$/, loader: 'ts-loader?cacheDirectory', include: [srcPath], exclude: [serverPath] },
            {test: /\.tsx$/, loader: 'ts-loader?cacheDirectory', include: [srcPath], exclude: [serverPath] },
            {test: /\.scss$/, loaders: ['style', 'css', 'sass']},
            {test: /\.png$/, loader: 'file-loader'},
            {test: /\.jpg$/, loader: 'file-loader'},
            {test: /\.jpeg$/, loader: 'file-loader'},
            {test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'file-loader?mimetype=image/svg+xml'},
            {test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/font-woff"},
            {test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/font-woff"},
            {test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/octet-stream"},
            {test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader"}
        ]
    },
    plugins:
    [
        new CopyWebpackPlugin
        ([
            { from: path.join(srcPath, 'images'), to: 'images' }
        ]),
        new webpack.optimize.CommonsChunkPlugin('lib', 'lib.js'),
        new HtmlWebpackPlugin
        ({
            inject: true,
            template: path.join(srcPath, 'index.html')
        }),
        new webpack.NoErrorsPlugin()
    ]
};

Source: (StackOverflow)

JQuery Typescript definitions using 'bool' instead of 'Boolean'

I got the "Definitely Typed" typescript definition file for JQuery and it uses bool instead of 'Boolean'.

This is giving me errors in my typescript project: Cannot find name 'bool'

I changed my version to use Boolean and it works, but it has me confused.

Is Definitely Typed still the place to get typescript definitions? If so, why is it not breaking for everyone else?

Is there a better place to get typescript definitions?


Source: (StackOverflow)

Why am I not able to compile a file that references jquery.d.ts? [duplicate]

This question already has an answer here:

The contents of my file, "blah.ts" are

///<reference path="jquery.d.ts">

and that's it.

The file, "jquery.d.ts" is on the same level as "blah.ts".

I copy and pasted jquery.d.ts from here

I installed typescript using npm and it is version 1.3.0.0.

When I run

tsc blah.ts

I get a page of errors

query.d.ts(279,40): error TS1005: ',' expected.
jquery.d.ts(279,61): error TS1005: '=' expected.
jquery.d.ts(279,64): error TS1109: Expression expected.
jquery.d.ts(279,97): error TS1005: ',' expected.
jquery.d.ts(279,118): error TS1005: '=' expected.
jquery.d.ts(279,121): error TS1109: Expression expected.
jquery.d.ts(342,40): error TS1005: ',' expected.
jquery.d.ts(342,61): error TS1005: '=' expected.
jquery.d.ts(342,64): error TS1109: Expression expected.
jquery.d.ts(342,103): error TS1005: ',' expected.
jquery.d.ts(342,124): error TS1005: '=' expected.
jquery.d.ts(342,127): error TS1109: Expression expected.
jquery.d.ts(352,48): error TS1005: ',' expected.
jquery.d.ts(352,69): error TS1005: '=' expected.
jquery.d.ts(352,72): error TS1109: Expression expected.
jquery.d.ts(352,111): error TS1005: ',' expected.
jquery.d.ts(352,132): error TS1005: '=' expected.
jquery.d.ts(352,135): error TS1109: Expression expected.
jquery.d.ts(677,35): error TS1005: ',' expected.
jquery.d.ts(677,36): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(692,39): error TS1005: ',' expected.
jquery.d.ts(692,40): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(722,36): error TS1005: ',' expected.
jquery.d.ts(722,37): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(744,41): error TS1005: ',' expected.
jquery.d.ts(744,42): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1214,36): error TS1005: ',' expected.
jquery.d.ts(1214,37): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1255,46): error TS1005: ',' expected.
jquery.d.ts(1311,45): error TS1005: ',' expected.
jquery.d.ts(1311,52): error TS1005: ',' expected.
jquery.d.ts(1383,22): error TS1005: ',' expected.
jquery.d.ts(1383,29): error TS1005: '=' expected.
jquery.d.ts(1404,44): error TS1005: ',' expected.
jquery.d.ts(1411,78): error TS1005: ',' expected.
jquery.d.ts(1428,25): error TS1005: ',' expected.
jquery.d.ts(1434,59): error TS1005: ',' expected.
jquery.d.ts(1446,31): error TS1005: ',' expected.
jquery.d.ts(1458,29): error TS1005: ',' expected.
jquery.d.ts(1489,30): error TS1005: ',' expected.
jquery.d.ts(1503,29): error TS1005: ',' expected.
jquery.d.ts(1541,24): error TS1005: ',' expected.
jquery.d.ts(1547,57): error TS1005: ',' expected.
jquery.d.ts(1615,50): error TS1005: ',' expected.
jquery.d.ts(1615,51): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1624,50): error TS1005: ',' expected.
jquery.d.ts(1624,51): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1647,29): error TS1005: ',' expected.
jquery.d.ts(1647,30): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1655,29): error TS1005: ',' expected.
jquery.d.ts(1655,30): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1669,30): error TS1005: ',' expected.
jquery.d.ts(1669,31): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1677,30): error TS1005: ',' expected.
jquery.d.ts(1677,31): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1692,28): error TS1005: ',' expected.
jquery.d.ts(1701,28): error TS1005: ',' expected.
jquery.d.ts(1709,33): error TS1005: ',' expected.
jquery.d.ts(1709,34): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1717,33): error TS1005: ',' expected.
jquery.d.ts(1717,34): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1738,27): error TS1005: ',' expected.
jquery.d.ts(1738,28): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1746,27): error TS1005: ',' expected.
jquery.d.ts(1746,28): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1760,27): error TS1005: ',' expected.
jquery.d.ts(1760,28): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1768,27): error TS1005: ',' expected.
jquery.d.ts(1768,28): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1782,32): error TS1005: ',' expected.
jquery.d.ts(1782,33): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1790,32): error TS1005: ',' expected.
jquery.d.ts(1790,33): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1804,34): error TS1005: ',' expected.
jquery.d.ts(1804,35): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1812,34): error TS1005: ',' expected.
jquery.d.ts(1812,35): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1826,30): error TS1005: ',' expected.
jquery.d.ts(1826,31): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1834,30): error TS1005: ',' expected.
jquery.d.ts(1834,31): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1864,29): error TS1005: ',' expected.
jquery.d.ts(1864,30): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(1872,29): error TS1005: ',' expected.
jquery.d.ts(1872,30): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(2464,55): error TS1005: ',' expected.
jquery.d.ts(2464,56): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(2471,62): error TS1005: ',' expected.
jquery.d.ts(2471,63): error TS1016: A required parameter cannot follow an optional parameter.
jquery.d.ts(2584,27): error TS1005: ',' expected.
jquery.d.ts(2584,31): error TS1005: '=' expected.
jquery.d.ts(2598,28): error TS1005: ',' expected.
jquery.d.ts(2598,32): error TS1005: '=' expected.
jquery.d.ts(2611,28): error TS1005: ',' expected.
jquery.d.ts(2611,32): error TS1005: '=' expected.
jquery.d.ts(2619,28): error TS1005: ',' expected.
jquery.d.ts(2619,32): error TS1005: '=' expected.
jquery.d.ts(2652,31): error TS1005: ',' expected.
jquery.d.ts(2652,35): error TS1005: '=' expected.
jquery.d.ts(2659,32): error TS1005: ',' expected.
jquery.d.ts(2659,36): error TS1005: '=' expected.
jquery.d.ts(2667,29): error TS1005: ',' expected.
jquery.d.ts(2667,33): error TS1005: '=' expected.
jquery.d.ts(2680,29): error TS1005: ',' expected.
jquery.d.ts(2680,33): error TS1005: '=' expected.
jquery.d.ts(2694,30): error TS1005: ',' expected.
jquery.d.ts(2694,34): error TS1005: '=' expected.
jquery.d.ts(2701,35): error TS1005: ',' expected.
jquery.d.ts(2701,39): error TS1005: '=' expected.
jquery.d.ts(2718,22): error TS1005: ',' expected.
jquery.d.ts(2718,29): error TS1005: ',' expected.
jquery.d.ts(2741,33): error TS1005: ',' expected.
jquery.d.ts(2741,41): error TS1005: ',' expected.
jquery.d.ts(2754,36): error TS1005: ',' expected.
jquery.d.ts(2754,44): error TS1005: ',' expected.
jquery.d.ts(2761,38): error TS1005: ',' expected.
jquery.d.ts(2761,46): error TS1005: ',' expected.
jquery.d.ts(2796,27): error TS1005: ',' expected.
jquery.d.ts(2796,34): error TS1005: ',' expected.

If I don't include jquery, it compiles fine (I did the tutorial and it worked). What is going wrong here?


Source: (StackOverflow)

Webstorm: Auto-completion still not working with LODASH (and others) after installing DefinitelyTyped

Using Webstorm 11.0.3 and Lodash 4.3.0. I have installed loadash DefinitelyTyped (see picture at the bottom) but am still not getting auto-completion for:

_.has

Do I need to do anything other than downloading and installing the DefinitelyTyped package as global?

TIA!

enter image description here


Source: (StackOverflow)

How long does it take for definitely-typed changes to appear in npm @types?

Lets say I change the typings on definitely-typed for foo and the PR is merged into master.

How long will it take for those changes to appear in @types/foo?


Source: (StackOverflow)