这里的Okey是我的代码,其中包含我尝试做的详细信息:
var str = "Hello m|sss sss|mmm ss";
//Now I separate them by "|"
var str1 = str.split("|");
//Now I want to get the first word of every split-ed sting parts:
for (var i = 0; i < codelines.length; i++) {
//What to do here to get the first word of every spilt
}
Run Code Online (Sandbox Code Playgroud)
那么我该怎么做:\我想得到的是firstword [0]会给出"Hello"而firstword [1]会给出"sss"而firstword [2]会给出"mmm"
我在 .tsx 文件中使用 Typescript 和 Material-ui 编写 ReactJS 类。在我的自定义组件之一中,我想创建对我在自定义组件中使用的组件之一的引用。
export class MyTextField extends React.Component<MyProps, MyState> {
private refTextField: React.RefObject<TextField>;
constructor(props: MyProps) {
super(props);
this.refTextField = React.createRef();
}
render(): JSX.Element {
const { id, label, value: defaultValue } = this.props;
const { value } = this.state;
const element = (
<TextField ref={this.refTextField} id={id} label={label} defaultValue={defaultValue} value={value} />
);
return element;
}
}
Run Code Online (Sandbox Code Playgroud)
在编译期间,我在引用的声明中收到错误消息:
'TextField' 指的是一个值,但在这里被用作一种类型。TS2749
我试图将“typeof TextField”放在我的声明中,但在我的渲染中评估 ref 属性时,我有另一条消息:
类型 'RefObject<(props: TextFieldProps) => Element>' 不能分配给类型 '((instance: HTMLDivElement | null) => void) …
我正在创建一个角度库,使用角度6,它基于角度材料,我需要包括锤子js库.
我知道在角度6中我们可以angular.json
在项目的配置下添加外部js库.但是这对于库来说不起作用.我尝试以下列方式添加外部库.
"my-library": {
"root": "projects/my-library",
"sourceRoot": "projects/my-library/src",
"projectType": "library",
"architect": {
"build": {
"builder": "@angular-devkit/build-ng-packagr:build",
"options": {
"tsConfig": "projects/my-library/tsconfig.lib.json",
"project": "projects/my-library/ng-package.json",
"scripts": [
"../node_modules/hammerjs/hammer.min.js"
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是我收到了这个错误.
架构验证失败,出现以下错误:数据路径""不应具有其他属性(脚本).
请建议在角度库中添加外部js文件的正确方法是什么.
我正在学习 Typescript-react,但我陷入了这个错误Type '({ items }: PropsWithChildren<TodoProps>) => Element[]' is not assignable to type 'FunctionComponent<TodoProps>'
,对此我迷失了。
完全错误:
Type '({ items }: PropsWithChildren<TodoProps>) => Element[]' is not assignable to type 'FunctionComponent<TodoProps>'.
Type 'Element[]' is missing the following properties from type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)>': type, props, key
Run Code Online (Sandbox Code Playgroud)
代码链接:sandbox repo。
文件中的TodoList
函数声明发生错误TodoList.tsx
。
任何帮助表示赞赏。干杯! …
我正在查看 Deno 并发现其中一个非常优雅的入门示例:
import { serve } from "https://deno.land/x/std@v0.2.10/http/server.ts";
const s = serve("0.0.0.0:8000");
void async function main() {
for await (const req of s) {
req.respond({ body: new TextEncoder().encode("Hello World\n") });
}
}()
Run Code Online (Sandbox Code Playgroud)
我正在使用 VS Code 编写 Deno 脚本,但我不确定如何让编辑器知道导入函数的类型,例如serve
. 我认为 Deno 对 IDE 的支持可能为时过早,但如果有解决方案或变通方法,我想了解一下。
我需要安装一个@types
包或类似的东西吗?import
或reference
一些.d.ts
声明文件?在使用serve
本示例中的函数以及 Deno 中的任何导入脚本时,如何让 VS Code 提供代码完成建议并显示类型注释?
我有一个任务。
拥有带有单spa框架的微前端。
所以我的问题只有一个:我不想复制像 react、react-dom(任何其他)这样的供应商库。我想让它们在其他微前端(与 webpack 捆绑在一起)之间共享
我知道拥有一些全局内容是什么不好的做法(这违反了与 webpack 捆绑的整个想法)。但是如何解决厂商库重复的问题呢?
我发现一个解决方案只是使用 SystemJs 加载规范,例如 html 中的分隔标签,但我只是想知道也许还有另一种解决方案。
谢谢你。
SystemJs 根据需求加载依赖项但从 CDN 加载依赖项的方法,我只想做同样的事情,但是从带有 react 和其他东西的“共享”webpack 包加载所有依赖项。
window.SystemJS = window.System
function insertNewImportMap(newMapJSON) {
const newScript = document.createElement('script')
newScript.type = 'systemjs-importmap'
newScript.text = JSON.stringify(newMapJSON)
const allMaps = document.querySelectorAll('script[type="systemjs-importmap"]')
allMaps[allMaps.length - 1].insertAdjacentElement(
'afterEnd',
newScript
)
}
const devDependencies = {
imports: {
react: 'https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.development.js',
'react-dom': 'https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.development.js',
'react-dom/server': 'https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom-server.browser.development.js',
'single-spa': 'https://unpkg.com/single-spa@4.3.2/lib/umd/single-spa.min.js',
lodash: 'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js',
rxjs: 'https://unpkg.com/rxjs@6.4.0/bundles/rxjs.umd.js',
}
}
const prodDependencies …
Run Code Online (Sandbox Code Playgroud) 既然没有必要package.json
用 deno来拥有文件,那么作为开发人员,我怎么能像npm scripts
在 in 中一样拥有类似的体验package.json
呢?
我试图抓取一个网站。我正在使用库https://github.com/request/request,但出现此错误:
Error: socket hang up
at createHangUpError (http.js:1476:15)
at Socket.socketOnEnd (http.js:1572:23)
at Socket.g (events.js:180:16)
at Socket.emit (events.js:117:20)
at _stream_readable.js:943:16
at process._tickDomainCallback (node.js:463:13) +0ms
Run Code Online (Sandbox Code Playgroud)
这里有什么问题?我也尝试重新安装该软件包,但我遇到了同样的错误......感谢帮助。
我正在构建一个小脚本,该脚本使用TypeScript Compiler API扫描具有给定类型成员的所有接口,其源代码可在此处找到。我检查这些类的成员,看看它们是如何相互关联的。
我的问题是:我怎么知道一种类型何时可以分配给另一种类型?我搜索了TypeChecker
一种方法,但找不到。有没有人有任何指示?这是一个应该能够分析的东西的例子:
export enum ASTKind {
Number,
Addition,
Multiplication,
}
export interface AST {
kind: ASTKind
}
export interface BinaryExpression extends AST {
left: AST
right: AST
}
export interface Addition extends BinaryExpression {
kind: ASTKind.Addition
}
export interface Multiplication extends BinaryExpression {
kind: ASTKind.Multiplication
}
Run Code Online (Sandbox Code Playgroud)
本质上,我想要一个谓词,说明是否ASTKind.Multiplication
可分配给ASTKind
(在这种情况下为真)。
打字稿抛出:
除非提供了“--jsx”标志,否则无法使用 JSX。ts(17004)
更改tsconfig.json
jsx
为 后react-jsx
,jsx 工作。yarn start
将 tsconfig.jsonreact-jsx
再次更改为。
react-scripts
已更新至 4.0.1。
包.json
"dependencies": {
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.1",
"semantic-ui-css": "^2.4.1",
},
"devDependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.2.2",
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^26.0.15",
"@types/node": "^14.14.9",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@typescript-eslint/eslint-plugin": "^4.8.1",
"@typescript-eslint/parser": "^4.8.1",
"eslint-config-react": "^1.1.7",
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-react-hooks": "^4.2.0",
"typescript": "^4.1.2"
},
Run Code Online (Sandbox Code Playgroud)
配置文件
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": …
Run Code Online (Sandbox Code Playgroud) typescript ×5
reactjs ×4
deno ×2
angular ×1
angular-cli ×1
angular6 ×1
architecture ×1
express ×1
javascript ×1
jsx ×1
material-ui ×1
node.js ×1
npm ×1
npm-scripts ×1
request ×1
scripting ×1
split ×1
tsx ×1
validation ×1
webpack ×1