What does "Type A has no properties in common with type B" mean?

14,184

TypeScript 2.4 introduced stronger checking of weak types, e.g. an interface where all properties are optional.

Suppose we have two weak types with different properties:

interface A {
    a?: string;
}

interface B {
    b?: string;
}

let x: A = {};
let y: B = {};

Notice that both x and y are empty objects that satisfy their respective weak types A and B.

Now, is it a mistake to assign an A to a B?

y = x;

TypeScript 2.4+ says yes, this is a mistake:

Type 'A' has no properties in common with type 'B'.

This is a simplified example; your typings file is certainly more complex but I hope this illustrates the intent of the error. If you post some code, we can dig into it further.

If TypeScript's weak type checking is being over-cautious in your case, there are workarounds such as casting or using an index signature:

https://blog.mariusschulz.com/2017/12/01/typescript-2-4-weak-type-detection

Share:
14,184
Maciej Szpakowski
Author by

Maciej Szpakowski

Learned C++ from course I downloaded from the Internet, then switched to c#. Studied C# at University of stackoverflow, School of dotnetperls.com and College of MSDN. Then, got BS in CS at UIC.

Updated on July 29, 2022

Comments

  • Maciej Szpakowski
    Maciej Szpakowski over 1 year

    After updating typescript from 2.3 to 2.6, I see this error in several of my typings. What does it actually mean ? Can you give me an example ?

    EDIT: I understand that this message indicates erroneous interface extension/implementation. I'm more interesting in the meaning of no properties in common. The suggested question shows an example of a class that implements an interface. What I've seen is an interface extending another interface and changing type of one of the properties. What does it have to do with the message ?