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 behavior when use union type #28860

Closed
MaoScut opened this issue Dec 5, 2018 · 5 comments
Closed

unexpected behavior when use union type #28860

MaoScut opened this issue Dec 5, 2018 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@MaoScut
Copy link

MaoScut commented Dec 5, 2018

TypeScript Version: 3.1.4

Search Terms:
union type
Code

export type basetype = {
  id: string;
}
export type supertype1 = basetype & {}
export type supertype2 = basetype & {
  key1: any;
  key2: any;
}

function myfun(p: supertype2 | supertype1) {
  return 
}

myfun( {
  id: '',
  key1: '',
})

Expected behavior:
{
id: '',
key1: '',
} is not supertype1 or supertype2, when it is used to be a parameter of myfun, typescript should report an error.
Actual behavior:
there is no error.

@j-oliveras
Copy link
Contributor

The myfun parameter is assignable to supertype1 (that is basically the same as basetype). Remember that the type are checked structurally.

Maybe that you expect is a exact type. See discussions about that: #202, #12936 and PR #28749.

@ahejlsberg
Copy link
Member

Write it like this instead:

export type basetype = { id: string }

export type supertype1 = basetype & { key1?: undefined, key2?: undefined }
export type supertype2 = basetype & { key1: any, key2: any; }

function myfun(p: supertype2 | supertype1) {}

myfun({ id: '' });  // Ok
myfun({ id: '', key1: '' });  // Error
myfun({ id: '', key1: '', key2: '' });  // Ok

@ahejlsberg ahejlsberg added the Working as Intended The behavior described is the intended behavior; this is not a bug label Dec 5, 2018
@MaoScut
Copy link
Author

MaoScut commented Dec 6, 2018

The myfun parameter is assignable to supertype1 (that is basically the same as basetype). Remember that the type are checked structurally.

Maybe that you expect is a exact type. See discussions about that: #202, #12936 and PR #28749.

I know structural typing, so I pass an object literal instead of a variable to myfun. And Exact type is what I need. Thank you.

@MaoScut
Copy link
Author

MaoScut commented Dec 6, 2018

Write it like this instead:

export type basetype = { id: string }

export type supertype1 = basetype & { key1?: undefined, key2?: undefined }
export type supertype2 = basetype & { key1: any, key2: any; }

function myfun(p: supertype2 | supertype1) {}

myfun({ id: '' });  // Ok
myfun({ id: '', key1: '' });  // Error
myfun({ id: '', key1: '', key2: '' });  // Ok

Nice. It work for me. Thank you.
interface { key1?: undefined}
describes key1 is a forbidden property. it's better to add this syntax to TypeScript documents.(handbook --> interface --> Optional Properties)

@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