0

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.