programing

속성이 '{}'유형에 없습니다.

luckcodes 2021. 1. 17. 11:30

속성이 '{}'유형에 없습니다.


완전히 패치 된 Visual Studio 2013을 사용하고 있습니다. 방금 만든 웹 사이트가 있으며 jquery, jqueryui 및 jsrender를 사용하려고합니다. 또한 TypeScript를 사용하려고합니다. TS 파일에서 다음과 같은 오류가 발생합니다.

'{}'유형에 'fadeDiv'속성이 없습니다.

나는 typescript에 대한 jquery, jqueryui 및 jsrender에 대해 생각하는 올바른 참조를 가지고 있지만 내가 읽은 내용에서 d.ts 문제처럼 보입니다. 누군가가 나를 도울 수 있기를 바랐습니다.

자바 스크립트에는 오류가 없지만 Visual Studio에서 내가 도울 수 있다면 오류가 있다고 말하는 것을 원하지 않습니다. 두 번 모두 fadeDiv가 자바 스크립트에 언급되어 있으며 그 아래에 빨간색 선이 있으며 두 오류 모두 위와 동일한 내용을 말합니다.

고마워 섀넌

/// <reference path="../scripts/typings/jquery/jquery.d.ts" />
/// <reference path="../scripts/typings/jqueryui/jqueryui.d.ts" />
/// <reference path="typings/jsrender/jsrender.d.ts" />

var SUCSS = {};

$(document).ready(function () {
   SUCSS.fadeDiv();
});

SUCSS.fadeDiv = function () {
var mFadeText: number;
$(function () {
    var mFade = "FadeText";
    //This part actually retrieves the info for the fadediv
    $.ajax({
        type: "POST",
        //url: "/js/General.aspx/_FadeDiv1",
        url: "/js/sucss/General.aspx/_FadeDivList",
        //data: "{'iInput':" + JSON.stringify(jInput) + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function (xhr, status, error) {
            // Show the error
            //alert(xhr.responseText);
        },
        success: function (msg) {
            mFadeText = msg.d.Fade;
            // Replace the div's content with the page method's return.
            if (msg.d.FadeType == 0) {//FadeDivType = List
                var template = $.templates("#theTmpl");
                var htmlOutput = template.render(msg.d);
                $("[id$=lblFadeDiv]").html(htmlOutput);
            }
            else {//FadeDivType = String
                $("[id$=lblFadeDiv]").html(msg.d.FadeDivString);
            }
        },
        complete: function () {
            if (mFadeText == 0) {
                $("[id$=lblFadeDiv]").fadeIn('slow').delay(5000).fadeOut('slow');
            }
        }
    });
});

나중에 이것을 읽을 수있는 사람들을 위해 .. SUCSS 네임 스페이스 .. 타이프 스크립트에서 나는 이와 같은 것을하고 싶었을 것 같다.

$(document).ready(function () {
    SUCSS.fadeDiv();
});
module SUCSS {
    export function fadeDiv () {};
};

따라서 함수는 내보내기를 사용하여 공개되며 SUCSS.fadeDiv ()를 사용하여 호출하여 페이지로드에서 실행되도록 SUCSS.fadeDiv를 호출 할 수 있습니다. 도움이 되길 바랍니다.


객체에 any 유형을 지정하기 만하면됩니다.

let bar = <any>{};
bar.foo = "foobar"; 

단일 필드에 대한 엄격한 유형 검사를 피하기 위해 배열 표기법으로 필드에 액세스합니다.

data['propertyName']; //will work even if data has not declared propertyName

모든 변수 필드에서 유형 검사를 완전히 비활성화 할 수도 있습니다.

let untypedVariable:any= <any>{}; //disable type checking while declaring the variable
untypedVariable.propertyName = anyValue; //any field in untypedVariable is assignable and readable without type checking

참고 : 모든 필드에 대한 모든 연속 액세스는 유형이 지정되지 않으므로 단일 필드 액세스에 대해서만 유형 검사를 피하는 것보다 더 위험합니다.


let propertyName= data['propertyName'];

TypeScript에서 다음 코드 줄을 작성할 때 :

var SUCSS = {};

의 유형은 SUCSS할당에서 유추됩니다 (즉, 빈 객체 유형 임).

You then go on to add a property to this type a few lines later:

SUCSS.fadeDiv = //...

And the compiler warns you that there is no property named fadeDiv on the SUCSS object (this kind of warning often helps you to catch a typo).

You can either... fix it by specifying the type of SUCSS (although this will prevent you from assigning {}, which doesn't satisfy the type you want):

var SUCSS : {fadeDiv: () => void;};

Or by assigning the full value in the first place and let TypeScript infer the types:

var SUCSS = {
    fadeDiv: function () {
        // Simplified version
        alert('Called my func');
    }
};

I suggest the following change

let propertyName =  {} as any;

Near the top of the file, you need to write var fadeDiv = ... instead of fadeDiv = ... so that the variable is actually declared.

The error "Property 'fadeDiv' does not exist on type '{}'." seems to be triggering on a line you haven't posted in your example (there is no access of a fadeDiv property anywhere in that snippet).


myFunction(
        contextParamers : {
            param1: any,
            param2: string
            param3: string          
        }){
          contextParamers.param1 = contextParamers.param1+ 'canChange';
          //contextParamers.param4 = "CannotChange";
          var contextParamers2 : any = contextParamers;// lost the typescript on the new object of type any
          contextParamers2.param4 =  'canChange';
          return contextParamers2;
      }

ReferenceURL : https://stackoverflow.com/questions/34274487/property-does-not-exists-on-type