How to define prop type which takes only true for typescript
srikanthkyatham opened this issue · comments
Hi
This is a cross post from rescript-forum. I didnot get an solution which worked. Seeking guidance.
https://forum.rescript-lang.org/t/how-to-define-prop-type-which-takes-only-true/1527
I am trying to write bindings for Textinput in the text-input.d.ts
indent preformatted text by 4 spaces
interface TextInputAutosizeProps
extends TextInputCoreProps,
Omit<TextareaAutosizeProps, 'children' | 'ref' | 'type'> {
autosize: true;
maxRows?: number;
rows?: number;
}
The bindings I have written so far
module TextInput = {
type textInputType = [#Email | #Number | #Password | #Tel | #Text | #Url]
type unitPosType = [#Inside | #Outside]
@genType.import(("@myorg", "TextInput")) @react.component
external make: (
~amount: bool=?,
~autosize: bool=?,
~changed: bool=?,
~disabled: bool=?,
~hasFocus: bool=?,
~invalid: bool=?,
~maxLength: int=?,
~maxRows: int=?,
~readonly: bool=?,
~rows: int=?,
~size: int=?,
~type_: @string
...
)=> React.element = "TextInput"
}
How to annotate autosize prop type. Please help
The solutions offered were
Solution1:
Use the @as attribute
module TextInput = { type textInputType = [#Email | #Number | #Password | #Tel | #Text | #Url] type unitPosType = [#Inside | #Outside] @genType.import(("@myorg", "TextInput")) @react.component external make: ( ~amount: bool=?, ~autosize: @as(json
true) _=?, }
the resultant type is
readonly autosize: unknown
Solution 2:
module Autosize: { type t let autosize: t } = { type t = bool let autosize = true }
the resultant type is
readonly autosize: Autosize_t;
export abstract class Autosize_t { protected opaque!: any }; /* simulate opaque types */
@srikanthkyatham here's an example: #545
If I understood the question correctly, it might do what you want.
It requires defining the type true_
somewhere in the project in a TS file.
@cristianoc yes it solves the issue. Thanks for the solution