type User = {
name: string;
};
const flag: any = false;
let list1: User;
list1 = flag ? flag : 1; // hope error
Why list1 can be 1, why not an error: Type '1' is not assignable to type 'User'
You are using any in the annotation to flag. any is by definition assignable to any type and assignable from any type. This means that the result of the expression flag ? flag : 1 will be any (since one of the results of the ternary expression is any the type of the expression is any | 1 which will get reduced to any).
Generally avoid any. If you really don't know a type at compile time use unknown. In this case removing the annotation yields an error as expected:
type User = {
name: string;
};
const flag = false; // no annotation
let list1: User;
list1 = flag ? flag : 1; // err
interface instead of type/class if you don't implement any methods on the interface u create (like domain objects with only the property definitions, or POJO in Java). It makes your class/type more lightweighttype and interface basically the same thing in this case. class is a different beast. But type and interface are both types that are erased at compile time (they differ in what can be done with them in the type system but for this simple example there is no practical difference)