typescript - "name ' 大小 ' does not exist in the current scope"即使我 've referenced it and it isn' t 突出显示?

标签 typescript

错误消息

Compile Error. See error list for details .../Scripts/gallery.ts(13,15): The name 'Size' does not exist in the current scope

画廊.ts

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

(function ($) {
    var $body = $(document.body);
    var $win = $(window);
    var animTime = 1000;
    var $cg = $('#control_grid').data('flash', false);

    function fitImage(img, max, enlarge) {
        var ratio = Math.max(img.width / max.width, img.height / max.height);
        if (ratio < 1 && !enlarge) ratio = 1;
        return Size(Math.round(img.width / ratio), Math.round(img.height / ratio));
    }

    ...

jquery.custom.js

function Size(w, h) {
    return {
        'width': w,
        'height': h
    };
}

...

问题

当我将鼠标悬停在 Visual Studio 中的 gallery.ts 中的“Size”上时,它会显示正确的定义,因此它可以清楚地找到该函数。为什么 TS 编译器会给我这个错误?

Size() 函数位于 jquery.custom.js 的根级别,它应该具有全局作用域,不是吗?

最佳答案

您不会通过引用 JavaScript 文件来获取类型信息。您可以在 TypeScript 中编写该函数,也可以为其创建一个定义文件,即 jquery.custom.d.ts

关于typescript - "name ' 大小 ' does not exist in the current scope"即使我 've referenced it and it isn' t 突出显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14045759/

相关文章:

javascript - react 汇总 : 'name' is not exported by node_modules/

javascript - 应用 array.filter 时,传递给函数的数组引用会丢失

javascript - 如何使用 defaultProps 定义 React 函数组件的接口(interface)而不引发丢失类型错误?

json - TypeScript 在不公开字段的情况下定义 getter

javascript - ValidationPipe() 不适用于 Nestjs/Crud 中的重写 @Query

javascript - 使用 Fluent API 增强 Typescript

reactjs - 如何通过 react 单元测试在多层自定义组件上触发事件

javascript - 如何在谷歌地图的屏幕中心设置标记

reactjs - 'React' 在定义之前就被使用了

javascript - 为什么 QullJS delta 出现乱序?