Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 具有Object.keys的用户定义类型保护_Typescript_Types - Fatal编程技术网

Typescript 具有Object.keys的用户定义类型保护

Typescript 具有Object.keys的用户定义类型保护,typescript,types,Typescript,Types,我按照TypeScript手册实现了用户定义的类型保护,但是我仍然会遇到一个错误,我不知道为什么。我觉得这和我使用Object.keys有关,但我不太确定 类型.ts 梅因酒店 及 这两个参数分别与proficiencesscore和percentageMatch函数调用的第一个参数survey1[category]有关。我想我已经正确地实现了我的用户定义类型保护(isProficienciesArray),我想知道问题出在哪里。我可以通过执行 const categoryScores: num

我按照TypeScript手册实现了用户定义的类型保护,但是我仍然会遇到一个错误,我不知道为什么。我觉得这和我使用Object.keys有关,但我不太确定

类型.ts

梅因酒店


这两个参数分别与
proficiencesscore
percentageMatch
函数调用的第一个参数
survey1[category]
有关。我想我已经正确地实现了我的用户定义类型保护(
isProficienciesArray
),我想知道问题出在哪里。

我可以通过执行

const categoryScores: number[] = Object.keys(survey1).map(category => {
    const x = survey1[category];
    const y = survey2[category];
    if (isProficienciesArray(x) && isProficienciesArray(y)) {
        return proficienciesScore(x, y) * categoryHighestScores[category];
    }
    else if (!isProficienciesArray(x) && !isProficienciesArray(y))
        return percentageMatch(x, y) * categoryHighestScores[category];
});

Aziz解决方案实现了有关TypeScript编译器限制的技巧

在国际海事组织,其他方法意味着更深入地修改代码。下一步可能是删除
if(isProficienciesArray…
/
else
语句(请参见规则2)。如果您喜欢这种代码,它将帮助TypeScript编译器进行类型推断,并改进代码

在下面的代码段中,它是使用字典/映射完成的,就像当前
类别highestscores
变量一样,但封装了分数/匹配计算:

  • proficienciesScore
    对于
    Proficiencies[]
  • 其他
    字符串[]
    数组的
    matchScore
该映射称为
matchFnMap
。函数
populateMatchFnMapWith()
有助于简化其创建

// types.ts
// [...]

const emptySurveyResponse: SurveyResponse = {
    devices: [],
    languages: [],
    frameworks: [],
    backends: [],
    proficiencies: []
};

export const surveyResponseCategories = Object.keys(emptySurveyResponse);

// main.ts
// [...]

interface MatchFn<T> {
    (a: T, b: T): number;
}

const matchFnMap: {[category: string]: MatchFn<SurveyResponse>} = {};

function populateMatchFnMapWith(category: string, categoryHighestScore: number, match: MatchFn<string[]|Proficiencies[]>) {
    matchFnMap[category] =
        (survey1: SurveyResponse, survey2: SurveyResponse) =>
            categoryHighestScore *
                match(survey1[category],
                      survey2[category]);
}

populateMatchFnMapWith('devices',       0.15, percentageMatch);
populateMatchFnMapWith('languages',     0.15, percentageMatch);
populateMatchFnMapWith('frameworks',    0.15, percentageMatch);
populateMatchFnMapWith('backends',      0.15, percentageMatch);
populateMatchFnMapWith('proficiencies', 0.40, proficienciesScore);

const matchScore = (survey1: SurveyResponse, survey2: SurveyResponse) =>
    surveyResponseCategories.reduce((total, category) => {
        const computeScore = matchFnMap[category];
        return total + computeScore(survey1, survey2);
    }, 0);
//types.ts
// [...]
const emptySurveyResponse:SurveyResponse={
设备:[],
语言:[],
框架:[],
后端:[],
熟练程度:[]
};
export const surveyResponseCategories=Object.keys(emptySurveyResponse);
//梅因酒店
// [...]
接口匹配fn{
(a:T,b:T):数字;
}
常量matchFnMap:{[类别:字符串]:MatchFn}={};
函数populateMatchFnMapWith(类别:string,categoryHighestScore:number,match:MatchFn){
地图[类别]=
(调查1:SurveyResponse,调查2:SurveyResponse)=>
分类高赫斯特分数*
匹配(调查1[类别],
调查2[类别];
}
使用('devices',0.15,percentageMatch)填充Match Fnmap;
populateMatchFnMapWith('languages',0.15,percentageMatch);
populateMatchFnMapWith('frameworks',0.15,percentageMatch);
用('backends',0.15,percentageMatch'填充Match Fnmap);
populateMatchFnMapWith('proficiencies',0.40,proficienciesScore);
const matchScore=(调查1:SurveyResponse,调查2:SurveyResponse)=>
调查响应类别。减少((总数,类别)=>{
const computeScore=matchFnMap[category];
返回总计+计算核心(测量1、测量2);
}, 0);

它仍然是函数式编程风格。进一步的步骤将意味着将模型修改为更面向对象的风格,以收集数据和计算。

。我想知道这是为什么。现在这是一个解决办法,但我真的很想弄清楚为什么我的代码不起作用。我开始认为这可能是一个打字错误。如果是的话,我会在他们的Github回购协议上报告。我想现在已经有一个问题了。
Argument of type 'string[] | Proficiencies[]' is not assignable to parameter of type 'Proficiencies[]'.
  Type 'string[]' is not assignable to type 'Proficiencies[]'.
    Type 'string' is not assignable to type 'Proficiencies'.
Argument of type 'string[] | Proficiencies[]' is not assignable to parameter of type 'string[]'.
  Type 'Proficiencies[]' is not assignable to type 'string[]'.
    Type 'Proficiencies' is not assignable to type 'string'.
const categoryScores: number[] = Object.keys(survey1).map(category => {
    const x = survey1[category];
    const y = survey2[category];
    if (isProficienciesArray(x) && isProficienciesArray(y)) {
        return proficienciesScore(x, y) * categoryHighestScores[category];
    }
    else if (!isProficienciesArray(x) && !isProficienciesArray(y))
        return percentageMatch(x, y) * categoryHighestScores[category];
});
// types.ts
// [...]

const emptySurveyResponse: SurveyResponse = {
    devices: [],
    languages: [],
    frameworks: [],
    backends: [],
    proficiencies: []
};

export const surveyResponseCategories = Object.keys(emptySurveyResponse);

// main.ts
// [...]

interface MatchFn<T> {
    (a: T, b: T): number;
}

const matchFnMap: {[category: string]: MatchFn<SurveyResponse>} = {};

function populateMatchFnMapWith(category: string, categoryHighestScore: number, match: MatchFn<string[]|Proficiencies[]>) {
    matchFnMap[category] =
        (survey1: SurveyResponse, survey2: SurveyResponse) =>
            categoryHighestScore *
                match(survey1[category],
                      survey2[category]);
}

populateMatchFnMapWith('devices',       0.15, percentageMatch);
populateMatchFnMapWith('languages',     0.15, percentageMatch);
populateMatchFnMapWith('frameworks',    0.15, percentageMatch);
populateMatchFnMapWith('backends',      0.15, percentageMatch);
populateMatchFnMapWith('proficiencies', 0.40, proficienciesScore);

const matchScore = (survey1: SurveyResponse, survey2: SurveyResponse) =>
    surveyResponseCategories.reduce((total, category) => {
        const computeScore = matchFnMap[category];
        return total + computeScore(survey1, survey2);
    }, 0);