You've given prop1 the wrong type. It should be:
class A {
static prop1: typeof Hey;
⋯
}
The type Hey refers to the instance type of the class Hey, whereas the type typeof Hey refers to the type of the Hey constructor value. Static properties exist on the constructor and not on instances, so prop1 will be of type typeof Hey, not of type Hey.
If this confuses you, you're not alone. Types and values in TypeScript exist in different namespaces, so you can have a value named X and a type named X and they don't necessarily have anything to do with each other, meaning that typeof X is a different type in general from X, so you have to be careful. See this answer to a similar question for a more in-depth discussion.
Anyway, once you make that change, things start working:
A.setFields()
console.log(A.prop1.a); // okay
Note that the problem was somewhat hidden from you because your Hey class has no instance members. An instance of Hey is indistinguishable in the type system from the empty object type {}, which allows nearly anything to be assigned to it. Empty classes behave strangely in TypeScript and are best avoided. If you have a class, it should probably have instance members. If Hey had even one random property, you'd have seen this error:
class Hey {
⋯
oops = 0; // actual property
}
class A {
static prop1: Hey;
static setFields() {
this.prop1 = Hey.setABC('aee', 'bee', 'cee'); // error!
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Property 'oops' is missing in type 'typeof Hey' but required in type 'Hey'.
return this
}
}
Note that if you never intend to construct instances of your class you can often get the desired behavior with plain objects. For example:
const Hey = {
a: "",
b: "",
c: "",
setABC(a: string, b: string, c: string) {
this.a = a
this.b = b
this.c = c
return this
}
}
const A = {
prop1: Hey,
setFields() {
this.prop1 = Hey.setABC('aee', 'bee', 'cee')
return this
}
}
A.setFields()
console.log(A.prop1.a); // okay
This behaves quite similarly to your code. One major difference is that I've initialized your properties whereas you left them undefined. But leaving properties uninitialized is a potential null/undefined error waiting to happen. Class instances have --strictPropertyInitialization but that doesn't (yet) apply to static members, although it's been requested at microsoft/TypeScript#27899.
You certainly don't have to refactor away from classes, but TS's support for empty classes with only static members isn't great, as you've seen.
Playground link to code
static prop1: typeof Hey, notstatic prop1: Hey, as shown here. The only reason why you didn't get an error insidesetFields()is because yourHeyhas an empty class instance, which is known to do weird things. Empty classes with only static members is a bit of an antipattern in TS, since the class type is just{}. Does that fully address the question? If so I'll write up an answer explaining; if not, what am I missing?typeof Heyis the type of the value namedHey, which is the constructor and all its static members, while the typeHeyis the type of instances of classHey, which does not have the static members (or any members in your case)classes if you have onlystaticmembers; see also here