Micah
Micah

Reputation: 4560

Typescript Error? or just typescript checking is not perfect?

I recently started to write typescript but have years experience with JS.


Example 1

Then while development I found that

const A : B = class B {}

Above will throw warning of "Cannot fine name'B'".

However, this will not.

class B {}

const A : B = B ;

Example 2

The other example is

class B {}

const A : B = B ;

const C : A = A ;

This will throw warning of "Cannot fine name'A'"


The result, tsc checking seems like only can recognize anything start from "class".

In JS perspective there is nothing wrong. Even compile with all of them will work....

So my question is, I am thinking this is just the tsc checking not good enough...., however, if I bypass this will lose the meaning of type checking, therefore I can only compromise my writing style probably need to drop it down.

Can you provide me some advise if I am wrong? or if there another reason for the error?

Upvotes: 0

Views: 48

Answers (1)

Trace
Trace

Reputation: 117

A class must be defined before you can use it as a type.

Also, in your third example you are attempting to use a constant as a type. This won't work as A does not have a class definition and therefore cannot be declared as a type. Read up on Typescripts docs on class and type definitions for a more detailed example.

Upvotes: 3

Related Questions