Skip to content

Excess properties not detected when assigning to union type containing a function type #8694

Description

The code below should throw a compiler error or am i missing something?

Code on Playground: https://www.typescriptlang.org/play

TypeScript Version:
[email protected], [email protected]

Code

type NormalType = string;
type WorksAsExpected = {a:number, c:number} | NormalType;

let works:WorksAsExpected = {a:1, c:2, d:3} 
/*erro.ts(4,40): error TS2322: Type '{ a: number; c: number; d: number; }' 
is not assignable to type '{ a: number; c: number; } | string'.
  Object literal may only specify known properties, and 'd' does not exist in type 
'{ a: number; c: number; } | string'.*/

type FunctionType = ()=>any;
type DoesntWork = {a:number, c:number} | FunctionType;

let worksTo:DoesntWork = 3 
/*erro.ts(9,5): error TS2322: Type 'number' is not assignable to type 
'{ a: number; c: number; } | (() => any)'.
  Type 'number' is not assignable to type '() => any'.
*/

let doesntWork:DoesntWork = {a:1, c:2, d:3} //no error is displayed

Expected behavior:
The third assignment should cause a compiler error
Actual behavior:
No error is displayed

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions