
type FromProps<T> = T extends AA<infer E> ? true : false;
class AA<E> {}
class CC {}
class BB {
aa: AA<string> = new AA
bb: CC = new CC
}
function get<K extends keyof BB>(a: K): FromProps<BB[K]> {
return null as unknown as FromProps<BB[K]>
}
let c = get("bb"); // true
Why is the type of c not false but true ???
>Solution :
Your 2 classes AA and CC are assignable to each other – both have no properties so for the typescript compiler, they’re interchangable.
If you want them to be unique, you have to add at least one unique property to each of them:
type FromProps<T> = T extends AA<infer E> ? true : false;
class AA<E> {
public id: number = 0;
}
class CC {
public name: string = 'Mike';
}
class BB {
aa: AA<string> = new AA
bb: CC = new CC
}
function get<K extends keyof BB>(a: K): FromProps<BB[K]> {
return null as unknown as FromProps<BB[K]>
}
let c = get("bb"); // false