인스턴스화 방법에 따라 정의가 다른 JS 클래스의 DefinitelyTyped TS 정의 파일을 구현하려고합니다. 정의는 잘 정의되어 있으며 인스턴스화 후에 변경되지 않습니다.
예 : 명확한 설명 :
EDIT 크게 감사합니다 모든 포인터가 될 것인스턴스에 따라 속성이 변경되는 클래스에 대한 TypeScript 정의 파일은 어떻게 만듭니 까?
interface myInterface{
c:string;
}
class MyClass("foo") implements myInterface{
a:string;
b:string;
c:string;
}
class MyClass("bar") implements myInterface{
c:string;
d:string;
e:string;
f:string;
}
:
var a = new MyClass("foo");
console.log(a.a);//"a"
console.log(a.b);//"b"
console.log(a.c);//"c"
console.log(a.d);//"undefined"
console.log(a.e);//"undefined"
console.log(a.f);//"undefined"
var b = new MyClass("bar");
console.log(b.a);//"undefined"
console.log(b.b);//"undefined"
console.log(b.c);//"c"
console.log(b.d);//"d"
console.log(b.e);//"e"
console.log(b.f);//"f"
내가 같은 TS 정의 뭔가 싶습니다 나는 또한 MyClass
을 수정할 수 없습니다를,있다 MyClass
을 인스턴스화하는 여러 가지 다른 방법이 있지만 모두 미리 알고 미리 정의되어 있습니다. 우리는 또한 MyClass
의 존재하지 않는 변형의 경우에 대해 걱정할 필요가 없다고 가정 할 수 있습니다.
또한 MyClass
은 데이터베이스에 대한 액세스를 허용하는 팩토리로 가장 잘 설명됩니다. 그래서 new MyClass("foo")
당신이 코드를 제어 할 경우 필드 이름이 이제 만들어 MyClass
객체
다른 경우 왜 동일한 클래스입니까? 왜 두 개의 다른 클래스를 만들지 않습니까? – Alex
'class MyClass ("foo") 구문은 myInterface {'가 올바르지 않습니다. –
'd ?: string;'처럼 물음표 ('?')를 찾으셨습니까? –