1

I would expect the following code to fail to transpile, as B should not be a valid type for array.push. What am I missing?

class A {};
class B {};
const arr: A[] = [];
arr.push(new B());
3

1 Answer 1

6

TypeScript uses structural compatibility to determine type compatibility. The classes are structurally compatible since they have the same properties (empty object).

See: https://www.typescriptlang.org/docs/handbook/type-compatibility.html

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

Comments

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.