将 Typescript 代码转换为 Javascript 代码?
Converting Typescript Code to Javascript Code?
我正在使用 Shopify's Node Api 教程创建 Redis 存储。但是,提供的代码块是打字稿,我的整个项目是用 javascript (React/nextjs) 编写的。我已经工作了几个小时,试图将代码转换为可用的代码,但无法让它在我的项目中正常工作。认真地为此苦苦挣扎。
我如何将下面的代码块从 typescript 转换为 javascript?
/* redis-store.ts */
// Import the Session type from the library, along with the Node redis package, and `promisify` from Node
import {Session} from '@shopify/shopify-api/dist/auth/session';
import redis from 'redis';
import {promisify} from 'util';
class RedisStore {
private client: redis.RedisClient;
private getAsync;
private setAsync;
private delAsync;
constructor() {
// Create a new redis client
this.client = redis.createClient();
// Use Node's `promisify` to have redis return a promise from the client methods
this.getAsync = promisify(this.client.get).bind(this.client);
this.setAsync = promisify(this.client.set).bind(this.client);
this.delAsync = promisify(this.client.del).bind(this.client);
}
/*
The storeCallback takes in the Session, and sets a stringified version of it on the redis store
This callback is used for BOTH saving new Sessions and updating existing Sessions.
If the session can be stored, return true
Otherwise, return false
*/
storeCallback = async (session: Session) => {
try {
// Inside our try, we use the `setAsync` method to save our session.
// This method returns a boolean (true if successful, false if not)
return await this.setAsync(session.id, JSON.stringify(session));
} catch (err) {
// throw errors, and handle them gracefully in your application
throw new Error(err);
}
};
/*
The loadCallback takes in the id, and uses the getAsync method to access the session data
If a stored session exists, it's parsed and returned
Otherwise, return undefined
*/
loadCallback = async (id: string) => {
try {
// Inside our try, we use `getAsync` to access the method by id
// If we receive data back, we parse and return it
// If not, we return `undefined`
let reply = await this.getAsync(id);
if (reply) {
return JSON.parse(reply);
} else {
return undefined;
}
} catch (err) {
throw new Error(err);
}
};
/*
The deleteCallback takes in the id, and uses the redis `del` method to delete it from the store
If the session can be deleted, return true
Otherwise, return false
*/
deleteCallback = async (id: string) => {
try {
// Inside our try, we use the `delAsync` method to delete our session.
// This method returns a boolean (true if successful, false if not)
return await this.delAsync(id);
} catch (err) {
throw new Error(err);
}
};
}
// Export the class
export default RedisStore;
你基本上需要摆脱所有类型(会话和字符串)并将 private
切换到 #
,可能是这样的:
/* redis-store.js */
import redis from 'redis';
import {promisify} from 'util';
class RedisStore {
#client;
#getAsync;
#setAsync;
#delAsync;
constructor() {
// Create a new redis client
this.client = redis.createClient();
this.getAsync = promisify(this.client.get).bind(this.client);
this.setAsync = promisify(this.client.set).bind(this.client);
this.delAsync = promisify(this.client.del).bind(this.client);
}
storeCallback = async (session) => {
try {
// Inside our try, we use the `setAsync` method to save our session.
// This method returns a boolean (true if successful, false if not)
return await this.setAsync(session.id, JSON.stringify(session));
} catch (err) {
// throw errors, and handle them gracefully in your application
throw new Error(err);
}
};
/*
The loadCallback takes in the id, and uses the getAsync method to access the session data
If a stored session exists, it's parsed and returned
Otherwise, return undefined
*/
loadCallback = async (id) => {
try {
// Inside our try, we use `getAsync` to access the method by id
// If we receive data back, we parse and return it
// If not, we return `undefined`
let reply = await this.getAsync(id);
if (reply) {
return JSON.parse(reply);
} else {
return undefined;
}
} catch (err) {
throw new Error(err);
}
};
/*
The deleteCallback takes in the id, and uses the redis `del` method to delete it from the store
If the session can be deleted, return true
Otherwise, return false
*/
deleteCallback = async (id) => {
try {
// Inside our try, we use the `delAsync` method to delete our session.
// This method returns a boolean (true if successful, false if not)
return await this.delAsync(id);
} catch (err) {
throw new Error(err);
}
};
}
// Export the class
export default RedisStore;
只需将所有 typescript 代码保存在 .ts 文件中(可能是 redis-store.ts)。
然后使用打字稿编译器通过 运行 tsc 命令转换为您的 javascript 版本,如下所示
tsc redis-store.ts
更多编译选项,请访问下方
https://www.typescriptlang.org/docs/handbook/compiler-options.html
我正在使用 Shopify's Node Api 教程创建 Redis 存储。但是,提供的代码块是打字稿,我的整个项目是用 javascript (React/nextjs) 编写的。我已经工作了几个小时,试图将代码转换为可用的代码,但无法让它在我的项目中正常工作。认真地为此苦苦挣扎。
我如何将下面的代码块从 typescript 转换为 javascript?
/* redis-store.ts */
// Import the Session type from the library, along with the Node redis package, and `promisify` from Node
import {Session} from '@shopify/shopify-api/dist/auth/session';
import redis from 'redis';
import {promisify} from 'util';
class RedisStore {
private client: redis.RedisClient;
private getAsync;
private setAsync;
private delAsync;
constructor() {
// Create a new redis client
this.client = redis.createClient();
// Use Node's `promisify` to have redis return a promise from the client methods
this.getAsync = promisify(this.client.get).bind(this.client);
this.setAsync = promisify(this.client.set).bind(this.client);
this.delAsync = promisify(this.client.del).bind(this.client);
}
/*
The storeCallback takes in the Session, and sets a stringified version of it on the redis store
This callback is used for BOTH saving new Sessions and updating existing Sessions.
If the session can be stored, return true
Otherwise, return false
*/
storeCallback = async (session: Session) => {
try {
// Inside our try, we use the `setAsync` method to save our session.
// This method returns a boolean (true if successful, false if not)
return await this.setAsync(session.id, JSON.stringify(session));
} catch (err) {
// throw errors, and handle them gracefully in your application
throw new Error(err);
}
};
/*
The loadCallback takes in the id, and uses the getAsync method to access the session data
If a stored session exists, it's parsed and returned
Otherwise, return undefined
*/
loadCallback = async (id: string) => {
try {
// Inside our try, we use `getAsync` to access the method by id
// If we receive data back, we parse and return it
// If not, we return `undefined`
let reply = await this.getAsync(id);
if (reply) {
return JSON.parse(reply);
} else {
return undefined;
}
} catch (err) {
throw new Error(err);
}
};
/*
The deleteCallback takes in the id, and uses the redis `del` method to delete it from the store
If the session can be deleted, return true
Otherwise, return false
*/
deleteCallback = async (id: string) => {
try {
// Inside our try, we use the `delAsync` method to delete our session.
// This method returns a boolean (true if successful, false if not)
return await this.delAsync(id);
} catch (err) {
throw new Error(err);
}
};
}
// Export the class
export default RedisStore;
你基本上需要摆脱所有类型(会话和字符串)并将 private
切换到 #
,可能是这样的:
/* redis-store.js */
import redis from 'redis';
import {promisify} from 'util';
class RedisStore {
#client;
#getAsync;
#setAsync;
#delAsync;
constructor() {
// Create a new redis client
this.client = redis.createClient();
this.getAsync = promisify(this.client.get).bind(this.client);
this.setAsync = promisify(this.client.set).bind(this.client);
this.delAsync = promisify(this.client.del).bind(this.client);
}
storeCallback = async (session) => {
try {
// Inside our try, we use the `setAsync` method to save our session.
// This method returns a boolean (true if successful, false if not)
return await this.setAsync(session.id, JSON.stringify(session));
} catch (err) {
// throw errors, and handle them gracefully in your application
throw new Error(err);
}
};
/*
The loadCallback takes in the id, and uses the getAsync method to access the session data
If a stored session exists, it's parsed and returned
Otherwise, return undefined
*/
loadCallback = async (id) => {
try {
// Inside our try, we use `getAsync` to access the method by id
// If we receive data back, we parse and return it
// If not, we return `undefined`
let reply = await this.getAsync(id);
if (reply) {
return JSON.parse(reply);
} else {
return undefined;
}
} catch (err) {
throw new Error(err);
}
};
/*
The deleteCallback takes in the id, and uses the redis `del` method to delete it from the store
If the session can be deleted, return true
Otherwise, return false
*/
deleteCallback = async (id) => {
try {
// Inside our try, we use the `delAsync` method to delete our session.
// This method returns a boolean (true if successful, false if not)
return await this.delAsync(id);
} catch (err) {
throw new Error(err);
}
};
}
// Export the class
export default RedisStore;
只需将所有 typescript 代码保存在 .ts 文件中(可能是 redis-store.ts)。 然后使用打字稿编译器通过 运行 tsc 命令转换为您的 javascript 版本,如下所示
tsc redis-store.ts
更多编译选项,请访问下方 https://www.typescriptlang.org/docs/handbook/compiler-options.html