Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unexpected behaviour when use Omit with Insection Types #39359

Closed
rust404 opened this issue Jul 1, 2020 · 3 comments
Closed

unexpected behaviour when use Omit with Insection Types #39359

rust404 opened this issue Jul 1, 2020 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@rust404
Copy link

rust404 commented Jul 1, 2020

TypeScript Version: 3.9.2

Search Terms:
omit, insection types

Code

interface User {
  type: 'user';
  name: string;
  age: number;
  occupation: string;
}

interface Admin {
  type: 'admin';
  name: string;
  age: number;
  role: string;
}

type PowerUser = Omit<User & Admin, 'type'> & {
  type: 'powerUser'
};
let p: PowerUser = {
  type: 'powerUser',
  name: 'hello',
  age: 123,
  occupation: '123',
  role: '123'
}
console.log(p)

Expected behavior:
no error

Actual behavior:

test.ts:21:3 - error TS2322: Type 'string' is not assignable to type 'never'.

21   name: 'hello',
     ~~~~

test.ts:22:3 - error TS2322: Type 'number' is not assignable to type 'never'.

22   age: 123,
     ~~~

test.ts:23:3 - error TS2322: Type 'string' is not assignable to type 'never'.

23   occupation: '123',
     ~~~~~~~~~~

test.ts:24:3 - error TS2322: Type 'string' is not assignable to type 'never'.

24   role: '123'
     ~~~~

Playground Link:
https://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=26&pc=15#code/LAKAlgdgLgpgTgMwIYGMYAICqBne6Deo66UAngA4wBc6A5AK65y0DcR6ESAttetlHEgBzNiGJIhvCPS4AjeKOIB7FCnrkkUMEog1+giCNABfUKEixEqDAEEAJl0gF2ZSjVpIHkVu0489AsKK6BJSMvJwwXBKADa8+kEmZiCuGAAKSgDu8Dh4ALzoAPKOUAA8uXDoAGTo9o4QADR0qbQAfNXOYiQUvLTkWTlMtCaicVDo5DQZ2XAV6AWEXanu-TMVtA2+3L0AFjAxMUob7KE0AIwATADMm10qahpaOu6XV8dd0XEv18MgpiAoHTYWIwAB0hyEAApyABKIA
Related Issues:

@dragomirtitian
Copy link
Contributor

dragomirtitian commented Jul 1, 2020

This is related to a recent change in behavior from #36696, where intersection resulting in a field of type never are reduced to never.

You can change your types to this:

type PowerUser = Omit<User,'type'> & Omit<Admin,'type'>  & {
  type: 'powerUser'
};

Playground Link

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jul 7, 2020
@RyanCavanaugh
Copy link
Member

@dragomirtitian has it right 👆

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants