Hi
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]
type trueType = [#True]
@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
Hongbo
April 27, 2021, 1:59pm
2
1 Like
woeps
April 28, 2021, 7:53pm
4
If you always want to set autosize
to true the previous examples are the way to go.
But if you want to distinct between not having it set and true
you could use an abstract type like this:
module Autosize: {
type t
let autosize: t
} =
{
type t = bool
let autosize = true
}
// and in your binding you'd use
// ~autosize: Autosize.t=?
This way you can either not set the prop or set it to true. But there is no way to set it to false.
I have seen similar APIs where false
would be ignored by the component. So it doesn’t really make sense to use false
, but nothing would break. I usually type these kind of props as bool because it’s just simpler and the overhead to type it “correctly” is not justified most of the time in my opinion.
2 Likes
@Hongbo
Thanks for the feedback the trick works if I use only js. But I am using @genType to convert to typescript the resultant type is
readonly autosize: unknown
with the above mentioned solution
@woeps
Thanks the solution. Using your solution the resultant type in the gen.tsx was
readonly autosize: Autosize_t;
export abstract class Autosize_t { protected opaque!: any }; /* simulate opaque types */
on the typescript side the type it was expecting was
readonly autosize: true;
Hi all,
For anyone looking for solution its here
opened 02:16PM - 10 May 21 UTC
Hi
This is a cross post from rescript-forum. I didnot get an solution which w… orked. 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 */
1 Like