Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
@AlCalzone

Description

@AlCalzone
Issue body actions

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?

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.