Skip to content

JSX props are not validated #3737

@prabirshrestha

Description

@prabirshrestha

//cc @RyanCavanaugh

Here is the code to reproduce.

Notice how I make a spelling mistake for props when using Message component. <Message mesage="World!" />. I would had expected TypeScript to give me a compile error here.

node ..\bin\tsc.js --jsx react -t es5 -m amd app.tsx typings\react\react.d.ts
/// <references file="./typings/react/react.d.ts" />

import * as React from 'react';

export interface MessageProps {
    message: string;
}

export class Message extends React.Component<MessageProps, any> {

    render() {
        return <div>Hello {this.props.message}</div>;
    }

}

export class App extends React.Component<any, any> {

    render() {
        return <Message mesage="World!" />
    }

}

Here is the generated code.

/// <references file="./typings/react/react.d.ts" />
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
define(["require", "exports", 'react'], function (require, exports, React) {
    var Message = (function (_super) {
        __extends(Message, _super);
        function Message() {
            _super.apply(this, arguments);
        }
        Message.prototype.render = function () {
            return React.createElement("div", null, "Hello ", this.props.message);
        };
        return Message;
    })(React.Component);
    exports.Message = Message;
    var App = (function (_super) {
        __extends(App, _super);
        function App() {
            _super.apply(this, arguments);
        }
        App.prototype.render = function () {
            return React.createElement(Message, {"messagex": "World!"});
        };
        return App;
    })(React.Component);
    exports.App = App;
});

Here is the definition of createElement from react.d.ts file.

    function createElement<P>(
        type: ComponentClass<P>,
        props?: P,
        ...children: ReactNode[]): ReactElement<P>;

React.createElement(Message, {"messagex": "World!"}); this could had been written in typescript as

React.createElement<MessageProps>(Message, <MessageProps>{"messagex": "World!"});

That would had lead to an error which could be caught during compile time.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions