seta为什么 TypeScript在不返回类型对象时接受定义A?
type A = {
a: '123',
b: '456'
}
// Returns copy of obj with obj[k] = '933'
function seta<K extends keyof A>(k: K, obj: A): A {
return {
...obj,
[k]: '933'
}
}
const w: A = seta('a', {
a: '123',
b: '456'
})
// now w = {a: '933', b: '456'}
Run Code Online (Sandbox Code Playgroud)
在使用 GDB 进行调试时,我注意到一些非常奇怪的事情。即, . 运算符在打印语句中使用时,可以像 -> 运算符一样取消引用单个甚至双指针以获取结构的字段。
这是一个简单的例子:
#include <stdio.h>
typedef struct node {
struct node *next;
int data;
} Node;
int main()
{
Node head, *head_p, **head_pp;
head.next = 0UL;
head.data = 42;
head_p = &head;
head_pp = &head_p;
// **GDB PAUSED HERE**
printf("head.data: %d\n", head.data);
// These won't compile because the . operator is misused
//
// printf("head_p.data: %d\n", head_p.data);
// printf("head_pp.data: %d\n", head_pp.data);
}
Run Code Online (Sandbox Code Playgroud)
这是 GDB 在暂停时打印的时间printf:
怎么可能。运算符(在 GDB 打印语句中)取消引用这样的指针??
谢谢!