And Import Statement"> And Import Statement">
Ad

What's The Different Between /// And Import Statement

- 1 answer

at first when I import react, the typescript compiler console 'can not find module react',then I add /// , and the problem fixed.

but I just found if I remove the reference comment, there is no error.

this is my code:

/**
 * Created by huagu on 2015/12/21.
 */
import * as ReactDOM from "react-dom";
import * as React from "react";
import FloatButton from "./FloatButton";

window.onload = function (event:Event) {
    var bd = document.querySelector('#bd');
    var style:__React.CSSProperties = {
        width:'80px',
        height:'40px',
        background:'blue',
        position:'absolute',
        top:'100px',
        left:'10px'
    }
    var str:__React.CSSProperties = '';
    var num:number = '';
    var c = <span style={{fontSize:'14px'}}>button</span>
    var props = {
        content:c,
        click:function(){
            console.log("FloatButton clicked");
        },
        style:style,
        allowSeriesClick:false,
    }
    ReactDOM.render(<FloatButton {...props}></FloatButton>, bd);
}

so, my problem is whether reference comment is necessary?I don't add reference comment, but there is no error reference __React.CSSProperties, it was declare in react.d.ts. if it's not necessary, why there is no error with "var str:__React.CSSProperties = ''; "?

enter image descript noreferrerenter image description here

Ad

Answer

///<reference> is the equivalent of saying "Just trust me that this will have run before my code", which is often something you can safely assume. It's intended for IDEs, and since it's a comment, does not affect code execution at all.

import modulename ... uses either commonjs or requirejs to actually retrieve the specified module before running the proceeding code.

If you have a dependency-management system that works with the module you need, it's preferred to use that instead of the former solution; that way, there is no risk of trying to access a module before it's loaded (even if 99% of uses aren't even remotely possible for the code to see before it has loaded)

Ad
source: stackoverflow.com
Ad