我试图将一个函数传递给一个子组件,但我收到了一些打字稿错误......
父.tsx
import React from 'react';
import {Child} from './child';
const Parent: React.FC = () => {
function fire() {
console.log('fire')
}
return (
<Child fire={fire}/>
// ^___ error here!
)
}
Run Code Online (Sandbox Code Playgroud)
错误fire是Type '{ fire: () => void; }' is not assignable to type 'IntrinsicAttributes & { children?: ReactNode; }'.
Property 'fire' does not exist on type 'IntrinsicAttributes & { children?: ReactNode; }'.ts(2322)
孩子.tsx
import React from 'react';
const Child: React.FC = (props: any) => {
return …Run Code Online (Sandbox Code Playgroud) 当我将数组保存到某个状态然后再次读取它时,我收到错误 Type 'string[]' is notassignable to type 'never[]' 这是怎么回事?
我在这里制作了一个代码沙箱:https : //codesandbox.io/s/affectate-pasteur-kj5h0代码运行但以红色下划线显示错误
作为 JSX 元素,一切都很好......
interface OwnProps {
children: JSX.Element;
location: Location;
}
export function Layout(props: OwnProps): JSX.Element {
Run Code Online (Sandbox Code Playgroud)
当我将其更改为功能组件时,出现错误 Layout
interface OwnProps {
children: JSX.Element;
location: Location;
}
const Layout: React.FC = (props: OwnProps) => {
Run Code Online (Sandbox Code Playgroud)
错误是
Type '(props: OwnProps) => Element' is not assignable to type 'FunctionComponent<{}>'.
Types of parameters 'props' and 'props' are incompatible.
Property 'location' is missing in type '{ children?: ReactNode; }' but required in type 'OwnProps'.ts(2322)
layout.tsx(18, 3): 'location' is declared here.
Run Code Online (Sandbox Code Playgroud) 我有一个基本的文件输入,我想允许用户上传整个目录
<input
type='file'
directory=''
webkitdirectory=''
className={cssClass}
onChange={processFiles}
/>
Run Code Online (Sandbox Code Playgroud)
这给出了打字稿错误
Type '{ type: "file"; directory: string; webkitdirectory: string; className: string; }' is not assignable to type 'DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>'.
Property 'directory' does not exist on type 'DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>'.ts(2322)
Run Code Online (Sandbox Code Playgroud) 我想跟踪和报告聊天室中的用户,我不确定如何最好地构建Firebase中的数据.
我们可以访问返回json的API,我计划每1分钟查看一次API查找所有聊天室(room_id),然后请求每个房间的所有用户(user_id).
数据的设置完全在我们的控制之下
我收到一个类型错误,我不知道如何正确构建界面
const data = [
{ category: 'fish', age: 10, color: 'red' },
{ category: 'fish', age: 9, color: 'red' },
{ category: 'fish', age: 8, color: 'blue' },
{ category: 'fish', age: 7, color: 'blue' },
{ category: 'birds', age: 10, color: 'red' },
{ category: 'birds', age: 9, color: 'red' },
{ category: 'birds', age: 8, color: 'blue' },
{ category: 'birds', age: 7, color: 'blue' },
];
interface CountProps {
category: string;
age: number;
color: string;
} …Run Code Online (Sandbox Code Playgroud) 我似乎能够创建画布,但是当我获得上下文时
const resizeImage = async (maxSize: number) => {
const image = originalImage;
const id = positionId;
const resizeCanvas = document.createElement('canvas');
resizeCanvas.width = maxSize;
resizeCanvas.height = maxSize;
const ctx: CanvasRenderingContext2D = resizeCanvas.getContext('2D');
// ^ Error
...
Run Code Online (Sandbox Code Playgroud)
错误是
Type 'CanvasRenderingContext2D | ImageBitmapRenderingContext | WebGLRenderingContext | WebGL2RenderingContext | null' is not assignable to type 'CanvasRenderingContext2D'.
Type 'null' is not assignable to type 'CanvasRenderingContext2D'.ts(2322)
Run Code Online (Sandbox Code Playgroud) 当我通过 mongoose 从 mongo 获取数据时......
const allAssets = await assets
.find({ company })
.sort([['createdAt', -1]])
.exec();
res.status(200).json({ assets: allAssets });
Run Code Online (Sandbox Code Playgroud)
我总是得到 _id 和 __v 但我不想将它们发送到前端,有没有办法可以轻松地说我不想要这些值或在发送它们之前删除它们?
{
"indistructable": true,
"_id": "5e345c2dc84be8995a5b4cf2",
"baseUri": "https://api.website.com/nameco8/",
"company": "jnameco8",
"description": "asset",
"image": "myimage.png",
"name": "jim",
"supply": 100,
"createdAt": "2020-01-31T16:56:13.816Z",
"updatedAt": "2020-01-31T16:56:13.816Z",
"__v": 0
},
Run Code Online (Sandbox Code Playgroud)
我尝试过添加
__v: {type: Number, select: false},
_id: { type: mongoose.Schema.Types.ObjectId, select: false },
Run Code Online (Sandbox Code Playgroud)
到我的架构,但是当保存到架构时,我收到错误
"document must have an _id before saving"
我有这个自定义钩子来调用 API
import { useState, useCallback } from 'react';
interface OptionsShape {
method: 'GET' | 'POST';
}
interface InitStateShape {
data: any;
success: boolean;
loading: boolean;
error: Error | null;
}
const useAPI = (initialData = null) => {
const initialState: InitStateShape = {
data: initialData,
success: false,
loading: false,
error: null,
};
const [response, setResponse] = useState(initialState);
const callAPI = async (URL: string, options: OptionsShape) => {
setResponse({ ...response, success: false, loading: true });
try {
const response = …Run Code Online (Sandbox Code Playgroud) 我正在尝试以流的形式发送图像......
const { Readable } = require("stream");
...
fastify.get(
"/v1/files/:cid",
async function (request: any, reply: any) {
const { cid }: { cid: string } = request.params;
const ipfs = create();
const readableStream = Readable({
async read() {
for await (const chunk of ipfs.cat(cid)) {
this.push(chunk);
}
this.push(null);
},
});
reply.type("image/png").send(readableStream);
}
);
Run Code Online (Sandbox Code Playgroud)
也尝试过
const readableStream = new Readable();
readableStream.read = () => {};
for await (const chunk of ipfs.cat(cid)) {
readableStream.push(chunk);
}
Run Code Online (Sandbox Code Playgroud)
我得到的信息是...
[17:31:38.006] INFO (44857): stream closed prematurely …Run Code Online (Sandbox Code Playgroud)