Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

angular - 使用TypeScript在registerOnChange中实现具有特定类型的ControlValueAccessor(Implementing ControlValueAccessor with specific type in registerOnChange using TypeScript)

Following a guide on implementing ControlValueAccessor , I noticed that the propagating function passed in the implementation of registerOnChange seems a bit loose.

(在遵循有关实现ControlValueAccessor的指南之后,我注意到在registerOnChange的实现中传递的传播函数似乎有点松动。)

private propagateChange = (_: any) => { };
public registerOnChange(fn: any) {
    this.propagateChange = fn;
}

I'm curious why the parameter passed in is any instead of something more strict describing an actual function (let alone a function with a predetermined signature).

(我很好奇为什么传入的参数是any参数,而不是更严格地描述实际函数的参数(更不用说具有预定签名的函数了)。)

After all, that's something that will be provided by the framework so it's predictable as to its contents, isn't it?

(毕竟,框架会提供某些内容,因此可以预测其内容,不是吗?)

Intuitively, I expected something type-restricted like this, instead.

(凭直觉,我期望这样的类型受限制 。)

public registerOnChange(fn: () => void) {
    this.propagateChange = fn;
}

Or, possibly, (with any preferably substituted for something more specific too).

(或者,可能的是(也最好用any代替更具体的东西)。)

public registerOnChange(fn: (param: any) => void) {
    this.propagateChange = fn;
}

Why isn't that the case in Angular?

(为什么在Angular中不是这种情况?)

Sloppy relaxing in design or a deeper issue I'm not seeing?

(草率的放松设计或我没有看到的更深层次的问题?)

  ask by Konrad Viltersten translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...