Cast object with mandatory property into object with same optional property


Cast object with mandatory property into object with same optional property



I'm trying wrap my head around why the following doesn't work:


/* @flow */

type A = {
foo: string
}

type B = {
foo?: string
}

const a: A = { foo: 'bar' };
const b: B = (a: B);



Flow gives me:


12: const b: B = (a: B);
^ Cannot cast `a` to `B` because string [1] is incompatible with undefined [2] in property `foo`.
References:
4: foo: string ^ [1]
8: foo?: string ^ [2]



All I'm trying to do is convert an object where a property is guaranteed to exist into an object where the property might exist - shouldn't that be ok?



Try Flow link here (no idea how long this will work)









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift