4

When I compile a simple React/Typescript application (shown below), I get a variety of compiler warnings that do not appear to be from within my code.

/// <reference path="main/ambient/react-dom/index.d.ts" />
/// <reference path="main/ambient/react/index.d.ts" />

import * as React from "react";
import { render } from "react-dom";

class Example extends React.Component<any, any> {
  render() {
    return <p> Hello! </p>;
  }
}

render(<Example />, document.getElementById("root"));

Although this example does run after compilation (via WebPack), it generates the following errors:

(19,22): error TS2339: Property 'createElement' does not exist on type '{}'.
(22,9): error TS2339: Property 'Component' does not exist on type '{}'.
(23,13): error TS2339: Property 'render' does not exist on type '{}'.
(23,26): error TS2339: Property 'createElement' does not exist on type '{}'.

I am running React v0.14.8. I am using these typings for React and these typings for React-dom.enter link description here

My question is: Why does this example generate so many errors? How can I resolve these errors?

Requested: tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "jsx": "react"
  },

  "exclude": [
    "node_modules",
    "typings/browser",
    "typings/browser.d.ts"
  ],
  "compileOnSave": false,
  "buildOnSave": false
}
4
  • Do you have a tsconfig.json file? Commented Apr 1, 2016 at 13:05
  • I have added the tsconfig.json for reference. Commented Apr 1, 2016 at 13:14
  • have you tried without the /// reference ? When using a tsconfig file you don't need those anymore. Also, you said it works with webpack, what version do you have of typescript installed globally? I'm assuming your getting the errors when running tsc Commented Apr 1, 2016 at 14:26
  • I was able to remove the compiler directives when used with webpack, but not with 'tsc'. tsc seems to work fine though, leading me to believe this might be an issue with webpack/ts-loader. Commented Apr 1, 2016 at 14:55

2 Answers 2

1

I found the solution. The bugs were caused by a plugin in WebPack (new webpack.optimize.OccurenceOrderPlugin()). Removing this plugin solved the problem.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to use typings and define the types for your dependencies. You can also refer a very nice implementation of using TypeScript with React here

1 Comment

I mentioned that I am using typings at the bottom of the question (using the exact tool you mention). Are those not correct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.