const s: string = 'foo';
const pass1 = (origin: string) => origin.concat(s);
const pass2 = (origin: string[]) => origin.concat(s);
type S = string | string[];
const error = (origin: S) => origin.concat(s);
Run Code Online (Sandbox Code Playgroud)
上面的代码。我可以调用concatastring或string[]type。那么,为什么打字稿禁止拨打concat的string | string[]类型?
错误是:
Cannot invoke an expression whose type lacks a call signature.
Type '((...strings: string[]) => string) | { (...items: ConcatArray<string>[]): string[]; (...items: (s...'
has no compatible call signatures.
Run Code Online (Sandbox Code Playgroud)
因为它们有不同的返回类型?但我认为 TS 可以推断出error的类型是S. …
代码来自Kilo项目:
/*
* Use write() and terminal escape (ESC) sequence
* to query cursor position.
*/
#include <stdio.h>
#include <unistd.h>
#define ESC 27
int main(int argc, char const *argv[]) {
char buf[32];
unsigned int i = 0;
int rows, cols;
write(STDOUT_FILENO, "\033[6n", 4);
while (i < sizeof(buf)-1) {
if (read(STDIN_FILENO,buf+i,1) != 1) break;
if (buf[i] == 'R') break;
i++;
}
buf[i] = '\0';
/* Parse it. */
if (buf[0] != ESC || buf[1] != '[') return -1;
if (sscanf(buf+2,"%d;%d",&rows,&cols) != …Run Code Online (Sandbox Code Playgroud)