TypeScript/tests/cases/compiler/typeVariableTypeGuards.ts

84 lines
1.3 KiB
TypeScript
Raw Normal View History

2017-05-04 06:28:17 +02:00
// @strict: true
// Repro from #14091
interface Foo {
foo(): void
}
class A<P extends Partial<Foo>> {
2017-11-16 19:58:12 +01:00
constructor(public props: Readonly<P>) {}
2017-05-04 06:28:17 +02:00
doSomething() {
this.props.foo && this.props.foo()
}
}
// Repro from #14415
interface Banana {
color: 'yellow';
}
class Monkey<T extends Banana | undefined> {
2017-11-16 20:08:03 +01:00
constructor(public a: T) {}
2017-05-04 06:28:17 +02:00
render() {
if (this.a) {
this.a.color;
}
}
}
interface BigBanana extends Banana {
}
class BigMonkey extends Monkey<BigBanana> {
render() {
if (this.a) {
this.a.color;
}
}
}
// Another repro
type Item = {
(): string;
x: string;
}
function f1<T extends Item | undefined>(obj: T) {
if (obj) {
obj.x;
obj["x"];
obj();
}
}
function f2<T extends Item | undefined>(obj: T | undefined) {
if (obj) {
obj.x;
obj["x"];
obj();
}
}
function f3<T extends Item | undefined>(obj: T | null) {
if (obj) {
obj.x;
obj["x"];
obj();
}
}
function f4<T extends string[] | undefined>(obj: T | undefined, x: number) {
if (obj) {
obj[x].length;
}
}
2017-05-04 19:20:13 +02:00
function f5<T, K extends keyof T>(obj: T | undefined, key: K) {
if (obj) {
obj[key];
}
}