TS그런 식으로 새 수업을 시작하는 방법 (예 C#: 내가 원하는 것을 보여주기 위해) :
// ... some code before
return new MyClass { Field1 = "ASD", Field2 = "QWE" };
// ... some code after
[편집]
이 질문을 쓸 때 JS 지식이 전혀없는 순수한 .NET 개발자였습니다. 또한 TypeScript는 완전히 새로운 것으로 C # 기반의 새로운 JavaScript 수퍼 셋으로 발표되었습니다. 오늘 나는이 질문이 얼마나 어리석은지를 봅니다.
어쨌든 누군가가 여전히 답을 찾고 있다면 아래 가능한 해결책을보십시오.
가장 먼저 주목해야 할 것은 TS에서 모델을위한 빈 클래스를 생성해서는 안된다는 것입니다. 더 나은 방법은 인터페이스 또는 유형을 만드는 것입니다 (필요에 따라 다름). Todd Motto의 좋은 기사 : https://ultimatecourses.com/blog/classes-vs-interfaces-in-typescript
해결 방법 1 :
type MyType = { prop1: string, prop2: string };
return <MyType> { prop1: '', prop2: '' };
해결책 2 :
type MyType = { prop1: string, prop2: string };
return { prop1: '', prop2: '' } as MyType;
솔루션 3 (실제로 수업이 필요할 때) :
class MyClass {
constructor(public data: { prop1: string, prop2: string }) {}
}
// ...
return new MyClass({ prop1: '', prop2: '' });
또는
class MyClass {
constructor(public prop1: string, public prop2: string) {}
}
// ...
return new MyClass('', '');
물론 두 경우 모두 함수 / 메소드 반환 유형에서 해결되므로 캐스팅 유형을 수동으로 필요로하지 않을 수 있습니다.
return new MyClass { Field1: "ASD", Field2: "QWE" };