-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
TypeScript Version: 2.9.2
Search Terms:
typescript return type based on parameter
Code
type StringOrNull<B extends Boolean> = B extends true ? string : (string | null)
type StringNotNull = StringOrNull<true>
function foo<B extends boolean>(returnString: B): StringOrNull<B> {
if (returnString) {
return '' as StringNotNull // type error: "Type 'string' is not assignable to type 'StringOrNull<B>'"
} else {
return null
}
}
Expected behavior:
Typecast should be valid, with the return type of foo
being:
string
whenreturnString
is passed astrue
string | null
whenreturnString
is passed as false
Actual behavior:
Cast cannot succeed, and return type is not dynamic based on the boolean passed to foo
Related Issues:
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed