简体   繁体   English

在 Typescript 中访问 json thought 接口

[英]Access json thougth interface in Typescript

I have this interface我有这个界面

interface User{
    name: String,
}

Then this method:然后这个方法:

printValue(path: WhatGoesHere?<User>){ // some type for helping autocomplete
    const JSON = {
        name: "Andres",
    }
    console.log(JSON[path]);
}

What i want is to have "intellisense" for sending the path param, so i want to do something like我想要的是有“智能感知”来发送路径参数,所以我想做类似的事情

printValue(User.name); // here intelisense shows and helps to write User.name

and get as result "Andres"并得到结果“安德烈斯”

Is this possible?这可能吗? thanks in advance提前致谢

interface User {
  name: String;
}

function printValue(path: keyof User) {
  const JSON = {
    name: "Andres",
  };
  console.log(JSON[path]);
}

printValue('name')

In this way you can access通过这种方式你可以访问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM