Skip to content

Can't assign the literal true to type true in certain contexts #29078

Closed
@cwohlman

Description

@cwohlman

Looks like similar issues have existed before, I'm getting an error where I can't assign the value true to a property with a type of true e.g, ComplicatedType | simpleType | { thisFieldShouldBeTrue: true }.

TypeScript Version: 3.3.0-dev.20181218

Search Terms:
true is not assignable to type boolean
boolean is not assignable to type true

Code

function get<ServerEndpoint extends { endpoint: string, execute: (...args: any) => any } | null>(params: FirstArgument<ServerEndpoint["execute"]>): ReturnValue<ServerEndpoint["execute"]> | { error: any } | { pending: true } {
    return {
      pending: true,
    };
}
  

type FirstArgument<T> = T extends (arg1: infer U, ...args: any[]) => any ? Partial<U> : any;
type ReturnValue<T extends (...args: any) => any> = 
  ReturnType<T> extends Promise<infer U> ? U :
  ReturnType<T>;

Expected behavior:
Compile successfully

Actual behavior:

$> npx tsc reproduction.ts
reproduction.ts:2:3 - error TS2322: Type '{ pending: boolean; }' is not assignable to type 'ReturnValue<ServerEndpoint["execute"]> | { error: any; } | { pending: true; }'.
  Type '{ pending: boolean; }' is not assignable to type '{ pending: true; }'.
    Types of property 'pending' are incompatible.
      Type 'boolean' is not assignable to type 'true'.

2   return {
    ~~~~~~~~
3     pending: true,
  ~~~~~~~~~~~~~~~~~~
4   };
  ~~~~


Found 1 error.

Playground Link: http://www.typescriptlang.org/play/#src=function%20get%3CServerEndpoint%20extends%20%7B%20endpoint%3A%20string%2C%20execute%3A%20(...args%3A%20any)%20%3D%3E%20any%20%7D%20%7C%20null%3E(params%3A%20FirstArgument%3CServerEndpoint%5B%22execute%22%5D%3E)%3A%20ReturnValue%3CServerEndpoint%5B%22execute%22%5D%3E%20%7C%20%7B%20error%3A%20any%20%7D%20%7C%20%7B%20pending%3A%20true%20%7D%20%7B%0D%0A%20%20%20%20return%20%7B%0D%0A%20%20%20%20%20%20pending%3A%20true%2C%0D%0A%20%20%20%20%7D%3B%0D%0A%7D%0D%0A%20%20%0D%0A%0D%0Atype%20FirstArgument%3CT%3E%20%3D%20T%20extends%20(arg1%3A%20infer%20U%2C%20...args%3A%20any%5B%5D)%20%3D%3E%20any%20%3F%20Partial%3CU%3E%20%3A%20any%3B%0D%0Atype%20ReturnValue%3CT%20extends%20(...args%3A%20any)%20%3D%3E%20any%3E%20%3D%20%0D%0A%20%20ReturnType%3CT%3E%20extends%20Promise%3Cinfer%20U%3E%20%3F%20U%20%3A%0D%0A%20%20ReturnType%3CT%3E%3B%0D%0A

Related Issues: #10432 #10657

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Contextual TypesThe issue relates to contextual types

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions