英文:
is it possible to convert yaml and properties interact using typescript
问题
我尝试编写一个小工具,可以让我使用TypeScript相互转换YAML和Properties。我尝试做的是这样的,首先安装依赖项:
npm install js-yaml properties
然后编写一个类似这样的函数:
const convertYamlToProperties = () => {
const yamlData = YAML.safeLoad('yaml字符串');
const properties = stringify(yamlData);
return (<div className="jwt-payload">{properties}</div>);
}
并像这样加载依赖项:
import * as YAML from 'js-yaml';
import { stringify } from 'properties';
也许"properties"库太旧,长时间没有维护,我无法顺利导入依赖项。在Visual Studio Code中显示以下错误:
Cannot find module 'properties' or its corresponding type declarations.ts(2307) No quick fixes available
React版本是18。要将YAML转换为Properties,你可以尝试升级"properties"库或使用其他替代库。以下是关键的版本信息:
"typescript": "^4.9.5",
"react": "^18.2.0"
英文:
I am tried to write a little tool that could let me convert the yaml and properties each other by using typescript. what I am tried to do like this, fisrt install dependencies:
npm install js-yaml properties
when write a function like this:
const convertYamlToProperties = () => {
const yamlData = YAML.safeLoad('yaml string');
const properties = stringify(yamlData);
return (<div className="jwt-payload">{properties}</div>);
}
and load the dependencies like this:
import * as YAML from 'js-yaml';
import { stringify } from 'properties';
maybe the properties is too old and did not maintain for a long time, I could not import the dependencies smoothly. Shows error in Visual Studio Code like this:
Cannot find module 'properties' or its corresponding type declarations.ts(2307)
No quick fixes available
The react version is 18. what should I do to convert the yaml to properties? this is the key version info:
"typescript": "^4.9.5",
"react": "^18.2.0",
答案1
得分: 1
没有现有的 @types/properties
在 NPM 上的定义。你可以自己提供类型。
你可以从 "any" 类型开始,以便立即使用它,但如果你想实现自动完成,你可能希望更加具体化你的类型。
这是 properties
中 stringify
函数的一个示例:
declare module 'properties' {
export interface StringifyOptions {
path: string;
comment: string
separator: string
unicode: boolean
replacer(this: {
isProperty: boolean;
isSection: boolean;
assert:() => any;
}, key: string, value: unknown, section: string | null);
}
export function stringify(obj: object, options?: Partial<StringifyOptions>, callback: (error: any, value: string) => void);
}
要么将此放入 .ts
文件中,要么放入 .d.ts
文件中,两者都应以某种方式导入到项目中,可以使用 import
或将其添加到 tsconfig.json
的 files
或 include
中。
英文:
There doesn't seem to be an existing @types/properties
definition on NPM. What you can do is supply your own types.
You can start with an "any" type to just allow you to use it immediately, but if you want to implement autocomplete you might want to be a bit more specific with your types.
Here is an example of the stringify
function in properties
:
declare module 'properties' {
export interface StringifyOptions {
path: string;
comment: string
separator: string
unicode: boolean
replacer(this: {
isProperty: boolean;
isSection: boolean;
assert:() => any;
}, key: string, value: unknown, section: string | null);
}
export function stringify(obj: object, options?: Partial<StringifyOptions>, callback: (error: any, value: string) => void);
}
Either put this in a .ts
file or a .d.ts
file, either of which should be imported into the project in some way, either by using import
or adding it into tsconfig.json
's files
or include
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论