小编Ara*_*edi的帖子

TypeScript:从字符串数组中定义联合类型

我不能成为遇到这个问题的第一个人,但我的搜索还没有发现任何有用的线索.非常感谢一些专家的TypeScript建议.

说我有一个数组:

const fruits = ["Apple", "Orange", "Pear"];
Run Code Online (Sandbox Code Playgroud)

我想定义一个对象,将每个水果映射到一些有趣的事实:

interface Facts {
    color: string,
    typicalWeight: number
}

const fruitFacts: { [key: members of fruits]: Facts } = {
    "Apple": { color: "green", typicalWeight: 150 }
    //
}
Run Code Online (Sandbox Code Playgroud)

我该怎么做[key: members of fruits]

额外:我如何强制我的fruitFacts对象耗尽从数组派生的所有键,以便它在上面的例子中指定苹果,橘子和梨的事实.

typescript

17
推荐指数
2
解决办法
3165
查看次数

字符串和字符串文字的 TypeScript 联合

我想为已知字符串文字之一或任何字符串创建类型定义,例如:

type IColor = "blue" | "green" | "red" | string;
Run Code Online (Sandbox Code Playgroud)

TS 不会抱怨该类型定义,但它也无助于智能感知。我的目标是定义一个接受已知颜色之一或任何颜色字符串的函数。

const knownColors = {
    green: "#66bb6a",
    red: "#ef9a9a",
    blue: "#81d4fa",
} as const;

function getColor(color: keyof typeof knownColors | string): string {
   if (color in knownColors) 
      return knownColors[color as keyof typeof knownColors];

   return color;
}
Run Code Online (Sandbox Code Playgroud)

用法:

getColor("blue");    // "blue" is a known color, returns "#81d4fa" 
getColor("black");   // "black" is not a known color, returns "black"
Run Code Online (Sandbox Code Playgroud)

我想让智能感知变得智能

getColor("_          // as I type this line, …
Run Code Online (Sandbox Code Playgroud)

typescript

5
推荐指数
2
解决办法
268
查看次数

在Node.js中解析PDF

我正在使用流星反应将PDF文档上传到我的Node.js后端,在这里我想以json或其他方式读取上传的PDF文档。可能吗?您会为此推荐什么库/工具?谢谢!

pdf-parsing node.js

4
推荐指数
1
解决办法
9904
查看次数

基于输入参数的TypeScript函数返回类型

我有一些不同的接口和对象,每个接口和对象都有一个type属性。假设这些是存储在NoSQL数据库中的对象。如何getItem基于输入参数创建具有确定性返回类型的泛型函数type

interface Circle {
    type: "circle";
    radius: number;
}

interface Square {
    type: "square";
    length: number;
}

const shapes: (Circle | Square)[] = [
    { type: "circle", radius: 1 },
    { type: "circle", radius: 2 },
    { type: "square", length: 10 }];

function getItems(type: "circle" | "square") {
    return shapes.filter(s => s.type == type);
    // Think of this as items coming from a database
    // I'd like the return type of this function to …
Run Code Online (Sandbox Code Playgroud)

typescript

4
推荐指数
3
解决办法
1133
查看次数

ReactJS-Youtube 数据 API 请求错误 400

我正在使用 YouTube 数据 API。我确实设置了参数,但是从开发人员工具打开网络时,它显示错误:“必需参数:部分”

axios 代码-

import axios from 'axios';

const KEY='[my key]'

export default axios.create({
    baseURL:'https://www.googleapis.com/youtube/v3',
    params:
    {
        part:'snippet',
        maxResults:5,
        key: KEY
    }
})
Run Code Online (Sandbox Code Playgroud)

发送 API 请求的回调函数

termSearch= (term)=>
    {   
        console.log(term)
        youtube.get('/search',{
            params:
            {
                q:term
            }
        })
Run Code Online (Sandbox Code Playgroud)

我希望收到 JSON 响应,但控制台显示 - Error:GET https://www.googleapis.com/youtube/v3/search?q=asdas 400

http-status-code-400 reactjs youtube-data-api axios

4
推荐指数
1
解决办法
1541
查看次数

Azure WebRole CloudConfigurationManager.GetSetting返回null

我有一个带有WebRole项目的Azure云解决方案.

Windows Azure Tools version: 2.2
Run Code Online (Sandbox Code Playgroud)

WebRole项目中的packages.config包含:

<package id="Microsoft.WindowsAzure.ConfigurationManager" version="2.0.2.0" targetFramework="net45" />
Run Code Online (Sandbox Code Playgroud)

我"Nuget Enable Package Restore"因此假设在构建期间链接和引用了正确的包.

以下行在我的开发盒Azure Compute模拟器上运行正常,但是,当我部署(签入TFS在线,构建解决方案并开始部署到Azure)时,此行返回null:

CloudConfigurationManager.GetSetting("StorageConnectionString")
Run Code Online (Sandbox Code Playgroud)

我已经阅读了其他几篇文章,但到目前为止还没有能够解决这个问题.我不知道某个包/ SDK不兼容的地方吗?

c# azure azure-cloud-services

3
推荐指数
1
解决办法
4412
查看次数

网站如何检测机器人?

我正在学习python,我正在抓reddit.不知怎的,reddit已经发现我是一个机器人(我的软件实际上是),但他们怎么知道呢?我们如何欺骗他们认为我们是普通用户.

我找到了实用的解决方案,但我要求更深入的理论理解.

python bots web-scraping web

-1
推荐指数
1
解决办法
2415
查看次数