Skip to content

Commit

Permalink
fix: ts type prompt error
Browse files Browse the repository at this point in the history
  • Loading branch information
yxw007 committed Sep 23, 2024
1 parent b1b712d commit e652359
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 59 deletions.
2 changes: 1 addition & 1 deletion esbuild.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const esbuild = require("esbuild");
import * as esbuild from "esbuild";

const production = process.argv.includes('--production');
const watch = process.argv.includes('--watch');
Expand Down
78 changes: 39 additions & 39 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"*"
],
"main": "./dist/extension.js",
"type": "module",
"icon": "icon.png",
"scripts": {
"vscode:prepublish": "npm run package",
Expand Down Expand Up @@ -89,7 +90,7 @@
"vsce": "^2.15.0"
},
"dependencies": {
"@yxw007/translate": "^0.0.8",
"@yxw007/translate": "^0.0.11",
"deepl-node": "^1.14.0"
},
"contributes": {
Expand Down
27 changes: 14 additions & 13 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode';
import { translator, engines, getLanguage, Engines, ToLanguage } from "@yxw007/translate";

const { translator, engines, getLanguage } = require("@yxw007/translate");
const pkg = require("../package.json");
const appName = normalName(pkg.name.split("-").slice(1).join("-"));
let originLanguages: string[] = [];
Expand All @@ -10,9 +10,9 @@ let registeredEngines: string[] = [];
const getConfigValue = (config: vscode.WorkspaceConfiguration, key: string): string => (config.get(key) ?? "");

function initTranslator() {
let google = engines.google()
let google = engines.google();
addEngine(google);
updateLanguageConfig(google.name);
updateLanguageConfig(google.name as Engines);
updateDefaultTargetLanguage(targetLanguages);
}

Expand Down Expand Up @@ -45,7 +45,7 @@ function registerEngine(engineName: string) {
region: getConfigValue(amazonConfig, "region"),
accessKeyId: getConfigValue(amazonConfig, "key_id"),
secretAccessKey: getConfigValue(amazonConfig, "access_key")
}))
}));
break;
}
case "baidu": {
Expand Down Expand Up @@ -129,10 +129,11 @@ function getTranslationPromise(editor: vscode.TextEditor, selectedText: string,
backgroundColor: "transparent"
});
editor.setDecorations(decoration, [selection]);
const engine = vscode.workspace.getConfiguration(appName).get("defaultEngine") as string;
translator.translate(selectedText, { to: targetLanguage, engine })
.then((res: string[]) => {
resolve({ selection, translation: res[0] });
const engine = vscode.workspace.getConfiguration(appName).get("defaultEngine") as Engines;
translator.translate(selectedText, { to: targetLanguage as ToLanguage<typeof engine>, engine })
.then((res) => {
const result = res as string[];
resolve({ selection, translation: result[0] as string });
decoration.dispose();
})
.catch((e: any) => {
Expand Down Expand Up @@ -265,7 +266,7 @@ async function choiceEngine(): Promise<string | undefined> {
return engine;
}

function updateLanguageConfig(engine: string) {
function updateLanguageConfig(engine: Engines) {
const languages = getLanguage(engine);
originLanguages = Object.keys(languages.from);
targetLanguages = Object.keys(languages.to);
Expand All @@ -274,14 +275,14 @@ function updateLanguageConfig(engine: string) {
function updateDefaultTargetLanguage(targetLanguages: string[]) {
const config = vscode.workspace.getConfiguration();
const englishIdx = targetLanguages.findIndex((key) => key.toLowerCase().indexOf("english") >= 0);
if (englishIdx == -1) {
if (englishIdx === -1) {
throw new Error('No English language found !');
}
const english = targetLanguages[englishIdx]
const english = targetLanguages[englishIdx];
config.update(`${appName}.targetLanguage`, english, vscode.ConfigurationTarget.Global);
}

function updateEngine(defaultEngine: string) {
function updateEngine(defaultEngine: Engines) {
registerEngine(defaultEngine);

vscode.workspace
Expand Down Expand Up @@ -360,7 +361,7 @@ async function handleSetDefaultEngine() {
return;
}

updateEngine(engine);
updateEngine(engine as Engines);

return true;
}
Expand Down
6 changes: 1 addition & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
],
"sourceMap": true,
"rootDir": "src",
"strict": true /* enable all strict type-checking options */
/* Additional Checks */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
"strict": true
}
}

0 comments on commit e652359

Please sign in to comment.