TypeScript가 할당 된 내용을 기반으로 암시 적으로 변수를 입력한다고 생각하십니까? 나는이 이상한 불일치를 얻고있다 - 나는 암시 적으로 할당하여 변수를 입력하고이 아닌 기존의 메소드를 호출이이 클래스의 내부에 발생하는 경우 TS는 오류가 발생하지 않지만 오류가 발생하는 경우이 클래스의 외부에서 발생하는 경우 :TypeScript의 암시 적 타이핑 불일치
class Bar {
constructor() {
//do nothing;
}
}
class Foo {
bar1;
bar2: Bar;
constructor() {
//do nothing;
this.bar1 = new Bar();
this.bar1.asdasd();
this.bar2 = new Bar();
this.bar2.asdasd(); //error TS2094: The property 'asdasd' does not exist on value of type 'Bar'.
}
}
var bar3 = new Bar();
bar3.asdasd(); //error TS2094: The property 'asdasd' does not exist on value of type 'Bar'.
var bar4 : Bar = new Bar();
bar4.asdasd(); //error TS2094: The property 'asdasd' does not exist on value of type 'Bar'.
bar1이 오류를 발생시키지 않는지 확인 하시겠습니까? 이 playground example을 확인하십시오.
상단 스 니펫에는 여러 가지 구문 오류가 있으며 두 번째 스 니펫은 오류를 발생시킵니다. ?. http://www.typescriptlang.org/Playground/#src=class%20Bar%20%7B%0A%20%20%20constructor()%20%7B%0A%20%20%20%2F%2Fdo%20 아무 것도 없음 % 3B % 0A % 20 % 20 % 20 % 7 % % 0 % % 7 % % 0 % % % 20 % % 20 % % 20 % % 20 % 20 % 20 % 20 % –
죄송합니다, 나는 서둘러서 타이핑 중이 었는데, 지금 고쳐졌습니다. 두 번째 코드 단편은 오류를 발생시키지 않습니다. –