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

Spread results in error with Interfaces #6854

Closed
dancrumb opened this issue Sep 6, 2018 · 2 comments
Closed

Spread results in error with Interfaces #6854

dancrumb opened this issue Sep 6, 2018 · 2 comments

Comments

@dancrumb
Copy link

dancrumb commented Sep 6, 2018

I would expect the following code to pass with no errors.

interface Baz {
  baz: string;
}

interface Foo extends Baz { 
  bar: number;
}

const foo: Foo = {bar: 2, baz: ''};
const qux: Foo = {...foo};        

However, it fails on the last line with

^ Cannot assign object literal to `qux` because property `baz` is missing in object literal [1] but exists in `Baz` [2].

I see comparable behaviour with Object.freeze and Object.assign.

Recreate: https://flow.org/try/#0JYOwLgpgTgZghgYwgAgEJwF7IN4ChnIBGmAXMgM5hSgDmA3LgL666iSyIoBiA9j8hAAekEABNyaTDmT4icKGRABXALaFoDZrgQ8QlZDD5le-ALw5iC5ACYANHIxkA5E8YMdesMgCOSwcb5kc2wAOjDDHjcCaOigA

@dancrumb
Copy link
Author

dancrumb commented Sep 6, 2018

I have seen similar issues concerning the spread operator in this repo, but they seem to focus on 'exact objects' and don't talk about interfaces.

@dancrumb
Copy link
Author

dancrumb commented Sep 6, 2018

FWIW, replacing the last line with this works:

const objectSafeClone: <S>(S) => S = function cloner<T>(o: T): T {
  return {...o};
};
  
const qux: Foo = objectSafeClone(foo);

@goodmind goodmind added this to the New Object Spread semantics milestone Jul 17, 2019
@dancrumb dancrumb closed this as not planned Won't fix, can't repro, duplicate, stale Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants