babel-plugin-tcomb icon indicating copy to clipboard operation
babel-plugin-tcomb copied to clipboard

Nullable propery should not allow to skip property

Open Riim opened this issue 8 years ago • 6 comments

https://yadi.sk/i/IMbvWSITyb5Hc

https://flowtype.org/try/#0PQKgBAAgZgNg9gdzCYAoVUCuA7AxgFwEs5swo44AKADwC4wBvVMMagfnrYEEAnHgQwCeAHn7ZBAPlQBfAJT0Azvh6FsAc0bMwPAKb5MPUgCIAJjqj9MMfGCUr1RgNwz05Kg1b1sVmGDnO3SgZ-VxwCYlI3ACYaeiYWOjBuPiFRcSk5RWVVDXjtPQNjMwsrGzscpxcMChiPRO8YXxDooJCgA

Riim avatar Nov 14 '16 12:11 Riim

Yes, babel-plugin-tcomb is more permissive than Flow in this regard. Basically there is the following equivalence

{ a?: string } <=> { a: void | string }

Is this an issue in practice?

gcanti avatar Nov 14 '16 15:11 gcanti

Is this an issue in practice?

Yes. Sorry for the translator, but I can not translate better. I use tcomb-validation for server response, null is fine on the server value is not specified and it sends null. The absence of the property-not the same thing, maybe it was someone renamed or on the server something is broken and I want to know about the problem at its very root, not in the form of a strange bug in the interface.

Original: Да. Извиняюсь за переводчик, но сам я переведу не лучше. Я использую tcomb для валидации ответа сервера, значение null -- это нормально, на сервере значение не задано и он присылает null. Отсутствие свойства -- не тоже самое, возможно оно было кем-то переименовано или на сервере что-то сломалось и я хочу знать о проблеме в самом её корне, а не в виде странного бага в интерфейсе.

Riim avatar Nov 14 '16 16:11 Riim

I see. Currently there's no support in tcomb (the library this plugin builds on) for optional fields (? syntax). I wonder if your issue could be fixed by an explicit union

function foo2(x: { x: null | Array<any> }): string {
  return 'default string'
}

gcanti avatar Nov 14 '16 16:11 gcanti

@gcanti could you move this example into your docs? ( If you want I can make a PR ) under a section for "Multiple Types and OR Types" or something along those lines I feel it would be helpful to others as for my use case, I have an error handler that expects a string or an object, and this was what I needed to solve that problem. ( yes, this is not related to the OPs question directly, but the only resource for this question I had )

hodonsky avatar Nov 07 '17 00:11 hodonsky

If you want I can make a PR

Ok, thanks

gcanti avatar Nov 07 '17 04:11 gcanti

I will get to this this weekend I think... Cheers mate!

hodonsky avatar Nov 09 '17 22:11 hodonsky