Skip to content

Is it possible to treat {value: A} | {value: B} as {value: A | B} ? #13263

@hax

Description

@hax

TypeScript Version: 2.1.4

Code

function or<A, B>(a: A, b: B) {
	if (Math.random() < 0.5) return {value: a}
	else return {value: b}
}

function readValue<T>({value}: {value: T}) {
	return value
}

const numeric1 = or(1, '1')
const v = readValue(numeric1)

Expected behavior:
Compiled with no error.

Actual behavior:
Error on readValue call:
The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.

Workaround:
Explicitly add return type or<A, B>(): {value: A | B}.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions