¿Cuál es la forma correcta de arreglar esto? No quiero simplemente emitir o hacer una copia de la matriz, convirtiendo undefined en una cadena vacía o algo así. Supongo que es hacky. ¿Cuál es la forma correcta de arreglar esto?
Type 'ProcessEnv' is not assignable to type '{ [key: string]: string; }'. 'string' index signatures are incompatible. Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.ts(2322) node-pty.d.ts(45, 5): The expected type comes from property 'env' which is declared here on type 'IPtyForkOptions | IWindowsPtyForkOptions'código:
import { IPty, spawn } from 'node-pty'; ptyProcess = spawn(shell, [], { name: 'xterm-color', cols: 80, rows: 30, cwd: process.env.HOME, env: process.env // error comes from this assignment });EDITAR 1:
Actualmente estoy haciendo esto:
interface INonUndefinedEnv { [key: string]: string } const safeEnv = () => { let o:INonUndefinedEnv = {}; for(const [key, value] of Object.keys(process.env).entries()) o[key] = value ?? ""; // make sure it's string and never undefined return o; }entonces puedo hacer:
ptyProcess = spawn(shell, [], { name: 'xterm-color', cols: 80, rows: 30, cwd: process.env.HOME, env: safeEnv() // type match });Pero me gustaría evitar todo este bucle si es posible...
Creo que la forma en que lo hiciste no es hacky. Es la cosa justa que hacer. Lo más complicado sería buscar en el código fuente de node-pty para ver si los valores indefinidos podrían causar un problema y usar "como cualquiera" si no pudieran. :)