3 ms·
This is in no way equal to having a 'class A { ... }' that you'd instantiate with 'new A()'. In module b, the const a will not be of 'type A', but a plain objec
by TomMarius 9y ago
This is in no way equal to having a 'class A { ... }' that you'd instantiate with 'new A()'. In module b, the const a will not be of 'type A', but a plain object with signature of '{ a: string }' for which A is an alias. That's very different because a plain object is not an instance - you can't use instanceof, try to console.log() it and an instance of a class and you'll see the difference.
- the_gipsy 9y agoI didn't mean that it's equal to having `class A {...}`. What I said is that you can export a type and a function with the same name, which is as convenient as importing a class, which is also both a type and a constructor function (edit: with prototype). By "constructor function" I meant a function that constructs an object with a given shape, not a "class instance" or prototyped object. Sorry about not being clear. So no `instanceof`, but you are guaranteed to get correct type checking. See here [1] for example, if you hover `a` it will tell you it is an `A` not a `{a:string}`. [1]: https://www.typescriptlang.org/play/#src=type%20MapToProps%3CT%3E%20=%20()%20=%3E%20T;%0D%0Atype%20Component%3CT%3E%20=%20(props:%20T)%20=%3E%20any;%0D%0Adeclare%20const%20connect:%20%3CT,%20U%3E(%0D%0A%20%20a:%20MapToProps%3CT%3E,%0D%0A%20%20b:%20MapToProps%3CU%3E%0D%0A)%20=%3E%20(c:%20Component%3CT%20&%20U%3E)%20=%3E%20any;;%0D%0A%0D%0Aconst%20A%20=%20()%20=%3E%20({%20a:%201%20});%0D%0Aconst%20B%20=%20()%20=%3E%20({%0D%0A%20%20%20%20x:%201,%0D%0A%20%20%20%20//%20b:%203,%0D%0A});%0D%0A%0D%0Atype%20I%20=%20{%20a:%20number,%20b:%20number%20};%0D%0A%0D%0Aconst%20MyComponent%20=%20(props:%20I)%20=%3E%20'MyJsx';%0D%0A%0D%0Aconnect(A,%20B)(MyComponent);%0D%0A https://www.typescriptlang.org/play/#src=type%20MapToProps%3...
- TomMarius 9y agoYes, Typescript will tell you that it's of type A, but that's not what happens at runtime - that's what I wanted to say. During runtime it's a plain object that has the aforementioned signature.