0

Right now I have this situation, a generic function type, an interface, and a function:

export type EVCb<T> = (err: any, val: T) => void;

export interface RegistryData {
  exitCode: number,
  npmVersion: string
}

export const getLatestVersionFromNPMRegistry = function (dir: string, name: string, cb: EVCb<RegistryData>) {

  const k = cp.spawn('bash', [], {
    cwd: dir
  });

  k.stdin.end(`npm view ${name}@latest version;`);

  const result : RegistryData = {
    exitCode: null,
    npmVersion: ''
  };

  k.stderr.setEncoding('utf8');
  k.stderr.pipe(process.stderr);

  k.stdout.on('data', d => {
    result.npmVersion = result.npmVersion += String(d || '').trim();
  });

  k.once('exit', code => {
    result.exitCode = code;
    cb(code, result);
  })

};

what I am looking to do, is obviate the need for the RegistryData interface. Is there some way I can remove the need to declare a separate interface, and just use the type information from the variable named result?

Basically, what I am looking to do is something like this:

export type EVCb<T> = (err: any, val: T) => void;

export const getLatestVersionFromNPMRegistry = function (dir: string, name: string, cb: EVCb<typeof result>) {

  const k = cp.spawn('bash', [], {
    cwd: dir
  });

  k.stdin.end(`npm view ${name}@latest version;`);

  const result = {
    exitCode: null,
    npmVersion: ''
  };

  k.stderr.setEncoding('utf8');
  k.stderr.pipe(process.stderr);

  k.stdout.on('data', d => {
    result.npmVersion = result.npmVersion += String(d || '').trim();
  });

  k.once('exit', code => {
    result.exitCode = code;
    cb(code, result);
  })

};

this is technically possible from tsc viewpoint, just not sure if it's a feature yet, is there some way to do this?

0 Answers0