Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

462
Views
La propiedad 'sutil' no existe en el tipo 'typeof webcrypto'

Estoy usando Node v17.4 y quiero consumir la API webcrypto. Basado en este ejemplo , estoy tratando de importar subtle en mi proyecto, pero TypeScript aparece con el error

La propiedad 'sutil' no existe en el tipo 'typeof webcrypto'.

Lo único que ofrece el espacio de nombres webcrypto es esto .

 import crypto from 'crypto'; const { subtle } = crypto.webcrypto; // subtle doesn't exist

¿Cómo obtengo acceso a subtle ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Como notó, @types/node incluye solo un código auxiliar para la interfaz webcrypto . Una solución consiste en ampliar la declaración del módulo crypto para declarar webcrypto.subtle como SubtleCrypto a partir de las definiciones de tipo DOM:

 // src/types/index.d.ts declare module "crypto" { namespace webcrypto { const subtle: SubtleCrypto; } }

Esto permite escribir un módulo como:

 // src/digest.ts: import * as crypto from "crypto"; // subtle has type SubtleCrypto, from the DOM type definitions const { subtle } = crypto.webcrypto; // Generate a cryptographic digest of the given message export async function digest(message?: string, algorithm = "SHA-512") { const encoder = new TextEncoder(); const data = encoder.encode(message); const hash = await subtle.digest(algorithm, data); return hash; }

Para usar SubtleCrypto , el proyecto debe habilitar las definiciones de tipo DOM agregando dom a la matriz lib en tsconfig.json . Por ejemplo, con los siguientes paquetes instalados:

 $ npm install -D typescript@4.6 @types/node@17 @tsconfig/node17

y un tsconfig.json que contiene:

 { "extends": "@tsconfig/node17/tsconfig.json", "compilerOptions": { "lib": ["dom"], "outDir": "dist", "typeRoots": ["./node_modules/@types", "./src/types"] }, "include": ["src/**/*"], "exclude": ["dist", "node_modules"] }

puede escribir un punto de entrada que llame al digest e imprima el resultado:

 // src/index.ts import { digest } from "./digest"; const message = "Hello, World!"; (async () => { const digestBuffer = await digest(message); console.log(Buffer.from(new Uint8Array(digestBuffer)).toString("hex")); })();

Esto compila y se ejecuta, como:

 $ npx tsc && node dist/index.js 374d794a95cdcfd8b35993185fef9ba368f160d8daf432d08ba9f1ed1e5abe6cc69291e0fa2fe0006a52570ef18c19def4e617c33ce52ef0a6e5fbe318cb0387
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!