Skip to content

Type 'true' is not assignable to type 'T2 extends keyof T1 ? true : false' #22735

Closed
@AlCalzone

Description

@AlCalzone

TypeScript Version: 2.8.0-dev.20180320

Search Terms: conditional type assignable

Code

function foo<T1, T2 extends string>(target: T1, prop: T2): T2 extends keyof T1 ? true : false {
	if (prop in target) return true;
	return false; // doesn't matter if this line is here or not.
}
// or
function foo<T1, T2 extends string>(target: T1, prop: T2): T2 extends keyof T1 ? true : false {
	return true;
}
// or
function foo<T1, T2 extends string>(target: T1, prop: T2): T2 extends keyof T1 ? true : null {
	if (prop in target) return true;
}

Expected behavior:
It works

Actual behavior:
error TS2322: Type 'true' is not assignable to type 'T2 extends keyof T1 ? true : false'.

This repro is a very reduced version of some code I'm trying to type with the new conditional syntax. Basically I want to achieve the following:

  • Case A: prop is a member of a specific union (here: the keys/property names of target) => the function returns a value of type 1
  • Case B: prop is NOT a member of that union => the function returns null

Am I misunderstanding something obvious here or is what I'm trying to do simply not possible?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions