此代码适用于 JavaScript:
var timeFormat = d3.timeFormat("%M:%S");
var yAxis = d3.axisLeft(y).tickFormat(timeFormat)
Run Code Online (Sandbox Code Playgroud)
但是 TypeScript 中的这段代码不起作用:
const yAxis = d3.axisLeft(y).tickFormat(d3.timeFormat("%M:%S"));
Run Code Online (Sandbox Code Playgroud)
function timeFormat(specifier: string): (date: Date) => string 返回给定字符串说明符的新格式化程序。返回的函数格式化指定的日期,返回相应的字符串。
默认语言环境中 locale.format (TimeLocaleObject.format) 的别名。
@param 说明符 — 日期格式的说明符字符串。
错误是
'(date: Date) => string' 类型的参数不能分配给 'null'.ts(2345) 类型的参数
我目前正在做 facebook relayjs 教程,我需要帮助理解教程的这一部分,它指出
接下来,让我们定义一个节点接口和类型。我们只需要为 Relay 提供一种方法来从对象映射到与该对象关联的 GraphQL 类型,以及从全局 ID 映射到它指向的对象
const {nodeInterface, nodeField} = nodeDefinitions(
(globalId) => {
const {type, id} = fromGlobalId(globalId);
if (type === 'Game') {
return getGame(id);
} else if (type === 'HidingSpot') {
return getHidingSpot(id);
} else {
return null;
}
},
(obj) => {
if (obj instanceof Game) {
return gameType;
} else if (obj instanceof HidingSpot) {
return hidingSpotType;
} else {
return null;
}
}
);
Run Code Online (Sandbox Code Playgroud)
在 nodeDefinition 的第一个参数上,它从哪里得到它的 globalId?Game 和 HidingSpot …
这是我的代码:
<Button
disabled={filter === 'Active'}
size='md'
color='primary'
name='Active' // complete = false
onClick={this.handleFilterClick}
>
Active
</Button>
Run Code Online (Sandbox Code Playgroud)
在我的函数处理程序上,我收到事件错误:
handleFilterClick = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
// continue here
const { name } = e.target;
Run Code Online (Sandbox Code Playgroud)
它说:
Property 'name' does not exist on type 'EventTarget'.
Run Code Online (Sandbox Code Playgroud)
我也试过:
(e: React.MouseEvent<HTMLInputElement, MouseEvent> & React.MouseEvent<HTMLButtonElement, MouseEvent>)
Run Code Online (Sandbox Code Playgroud)
按钮的事件类型是什么?在 JavaScript 中,这有效,它可以接受名称,但我不知道为什么不打字稿?
我有这个数组类型:
interface Details {
Name: string;
URL: string;
Year: number;
}
interface AppState {
data: Details[];
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 D3 创建一个这样的 x 轴:
createChart = () => {
const { data } = this.state;
const width = 900,
height = 600;
// x-axis
const x = d3
.scaleLinear()
.domain([
d3.min(data, ({ Year }) => (Year ? Year - 1 : 0)), // ERROR
d3.max(data, ({ Year }) => (Year ? Year + 1 : 0)) // ERROR
])
.range([0, width]);
}; …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用打字稿创建 d3 树形图 v5,但我无法将其从 js 转换,此代码适用于 js 但不适用于打字稿:
var cell = svg
.selectAll('g')
.data(root.leaves())
.enter()
.append('g')
.attr('class', 'group')
.attr('transform', function(d) {
console.log('d ', d)
return 'translate(' + d.x0 + ',' + d.y0 + ')';
});
Run Code Online (Sandbox Code Playgroud)
当我记录 d 时,这些属性 x0 和 y0 存在于 d arg 上,但我收到错误:
类型“HierarchyNode<{ id: string;”上不存在属性“x0” 名称:字符串;孩子们:从不[];}>'
我该如何进行这项工作?我不想使用任何东西,但我是打字稿新手?
我是个玩笑新手,我很难将组件中的测试与子组件集成:
const ComponentToTest = () =>
<Formik
{...{
initialValues: createBeneficiaryInitialValues,
onSubmit,
validationSchema: addPayoutAccountValidationSchema,
}}>
<CreateBeneficiaryForm />
</Formik>
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时:
it('it sends create Beneficiary mutation', () => {
render(
<RelayProvider {...{ environment }}>
<ComponentToTest />
</RelayProvider>
);
Run Code Online (Sandbox Code Playgroud)
当我运行 jest 时出现此错误:
上述错误发生在组件:ForwardRef(由CreateBeneficiaryForm创建)中
CreateBeneficiaryForm来自MyComponentToTest,为什么它会开玩笑地抛出错误?
新的在这里作出反应,不知道在这样的setState回调上做这个是否正确?
setTimeout(()=> {
this.setState((state, props) => ({ activateLightColorForRed: true }), () => {
setTimeout(()=> {
this.setState(()=> ({ activateLightColorForRed: false }))
}, 500);
red.play();
})
}, toWait);
Run Code Online (Sandbox Code Playgroud)
或者这样的事情?
this.setState((state, props) => {
this.setState((state, props) => {
activateLightColorForRed: true
});
setTimeout(() => { activateLightColorForRed: false },500)
})
Run Code Online (Sandbox Code Playgroud)
是否更新了setState回调的状态?在我的组件中发生了一些奇怪的事情,它会多次渲染.我不确定,但我认为这是因为我正在做第一个样本?
我想检测expo React Native中设备的当前方向,这是我的代码,不起作用:
import {
Dimensions,
} from 'react-native';
import * as ScreenOrientation from 'expo-screen-orientation';**
const App = () => {
...
useEffect(() => {
const isPortrait = () => {
const dimension = Dimensions.get('screen');
return dimension.height >= dimension.width;
};
Dimensions.addEventListener('change', () => {
const orientation = isPortrait() ? 'portrait' : 'landscape';
console.log('Dimensions orientation', orientation);
});
ScreenOrientation.addOrientationChangeListener((e) => {
console.log('e ', e);
});
}, []);
Run Code Online (Sandbox Code Playgroud)
当我旋转设备时,没有日志,所以它没有触发?
我不太确定如何调试此错误,因为我在终端上,我确定我的项目 ID 并且我已经使用我的 gcloud 登录。这是我的 -vdebug 错误:
INFO[0000] starting gRPC server on port 50051
INFO[0000] starting gRPC HTTP server on port 50052
INFO[0000] Skaffold &{Version:v1.12.0 ConfigVersion:skaffold/v2beta5 GitVersion: GitCommit:e680a831292e1c7efc54e0c6d40544ae141e6354 GitTreeState:clean BuildDate:2020-07-04T21:01:46Z GoVersion:go1.14.4 Compiler:gc Platform:darwin/amd64}
DEBU[0000] config version "skaffold/v2alpha3" out of date: upgrading to latest "skaffold/v2beta5"
DEBU[0000] validating yamltags of struct SkaffoldConfig
DEBU[0000] validating yamltags of struct Metadata
DEBU[0000] validating yamltags of struct Pipeline
DEBU[0000] validating yamltags of struct BuildConfig
DEBU[0000] validating yamltags of struct Artifact
DEBU[0000] validating yamltags of struct Sync …Run Code Online (Sandbox Code Playgroud) 我是React钩子的新手,但我在文档中看不到这一点:
const MyComponent = ({myProp}) => {
const [myPropHook, setPropHook] = useState(myProp)
...
}
Run Code Online (Sandbox Code Playgroud)
我想知道这是否是一个好习惯?
reactjs ×4
typescript ×4
d3.js ×3
javascript ×2
docker ×1
expo ×1
gcloud ×1
jestjs ×1
kubernetes ×1
react-native ×1
react-tsx ×1
relayjs ×1
skaffold ×1