Error using morphs in union types #1402
-
I'm trying to represent the following types: const TestScope = scope({
opts: {
active: 'boolean',
// the formats expected are ["en","it"] and "en|it", but I'd like to work with the former representation
'translations?': type('string|string[]').pipe((s): string[] =>
typeof s === 'string' ? s.split('|') : s
)
},
file: {
name: 'string',
'opts?': 'opts'
},
module: {
name: 'string',
files: 'file[]'
},
package: {
content: '(file|module)[]'
}
}).export() But i get the following error:
I'm aware of the limitations of morphs, but I was curios if there's a workaround or a different approach that could work. Thanks 😄 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The error here is essentially because if we get data that is a valid const data = {
name: "foo",
files: []
} If you add something to one of the types so there's no way they could overlap, it should work, e.g.: file: {
name: "string",
"opts?": "opts",
"files?": "never"
}, That said, we should actually allow this case because its the same morph being applied in each branch. There's an open issue #1375 tracking an array edge case that would allow your original implementation to work. |
Beta Was this translation helpful? Give feedback.
-
Thank you so much, i'll keep an eye on #1375! |
Beta Was this translation helpful? Give feedback.
The error here is essentially because if we get data that is a valid
file
andmodule
like:If you add something to one of the types so there's no way they could overlap, it should work, e.g.:
That said, we should actually allow this case because its the same morph being applied in each branch. There's an open issue #1375 tracking an array edge case that would allow your original implementation to work.