Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ts怎么限定参数是一个object对象

interface InitBody {
}

function report (eventId: string, body: InitBody) {}

report("333", "88888"); // 这里为什么不会报错

如何限定调用report方法的时候,第二个参数必须是object对象啊,例如传一个{a: 'aaa'},或者 {}都行啊


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

1 Answer

0 votes
by (71.8m points)

image.png

P.S. 看上去你这是要做事件上报,一般来说事件上报都是上报字典,Value 的类型也都是简单类型,不会有复杂的 Object 嵌套,你甚至可以直接声明成 [key: string]: string | number | boolean 这种。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...