我正在逐步将一个应用程序用 React to TypeScript 重写为 ReScript。我已经在 ReScript 中实现了几个组件,但这是第一个,我将其用作ReactDOM.Style.t组件的属性。
这是我的最小化组件代码:
@genType
@react.component
let make = (~sx: option<ReactDOM.Style.t>=?) => {
<div></div>
}
Run Code Online (Sandbox Code Playgroud)
ReScript 编译良好(除了未使用的警告sx,但我们可以忽略它)。
我生成了以下内容bs.js,看起来没问题:
@genType
@react.component
let make = (~sx: option<ReactDOM.Style.t>=?) => {
<div></div>
}
Run Code Online (Sandbox Code Playgroud)
.gen.tsx以及导致问题的以下相应文件:
/* TypeScript file generated from InhypedIcon.res by genType. */
/* eslint-disable import/first */
import * as React from 'react';
// @ts-ignore: Implicit any on import
import * as InhypedIconBS__Es6Import from './InhypedIcon.bs';
const InhypedIconBS: any = InhypedIconBS__Es6Import;
import type …Run Code Online (Sandbox Code Playgroud) 考虑到以下人为的示例,是否可以编写一个get可以处理具有a属性的任何记录的函数?
type type_one = {a: int}
type type_two = {a: int, b: int}
let example_one = {a: 1}
let example_two = {a: 1, b: 2}
let get = record => record.a
Js.log(get(example_one)) // notice the error here
Js.log(get(example_two))
Run Code Online (Sandbox Code Playgroud)
如果不是,这可以用对象实现吗?或者,处理这种情况的最佳方法是什么?
在 Rescript 中做出承诺时:
let myPromise = Js.Promise.make((~resolve, ~reject) => resolve(. 2))
Run Code Online (Sandbox Code Playgroud)
ReScript 编译器将发出警告 unused variable reject.
有没有办法抑制这个错误?
我一直在阅读 F# 文章,他们使用单个案例变体来创建不同的不兼容类型。但是在 Ocaml 中,我可以使用私有模块类型或抽象类型来创建不同的类型。在 Ocaml 中使用像 F# 或 Haskell 这样的单例变体是否很常见?