从数据对象到精简接口的打字稿

时间:2018-07-20 17:25:21

标签: typescript interface

问题描述

我的当前数据:

{
  "field1": "value",
  "field2": 3,
  "field3": true,
  "extraField": "toRemove"
}

我定义下一个接口:

export interface MyInterface {
  field1: string;
  field2: number;
  field3: boolean;
}

目标

我需要将当前数据转换或转换为MyInterface(删除 extraField

避免以下解决方案

  • 删除 myData.extraField ;
  • .map()运算符

1 个答案:

答案 0 :(得分:0)

$type = strtolower(readline());
$day = strtolower(readline());
$quantity = floatval(readline());    
$days = array(1 => "monday", "tuesday", "wednesday", "thursday", "friday");
$weekend = array(1 =>"saturday", "sunday");   
$fruit_normal = array(
    "2.50" => "banana",
    "1.20" => "apple",
    "0.85" => "orange",
    "1.45" => "grapefruit",
    "2.70" => "kiwi",
    "5.50" => "pineapple",
    "3.85" => "grapes"
   );
    $fruit_we = array(
    "2.70" => "banana",
    "1.25" => "apple",
    "0.90" => "orange",
    "1.60" => "grapefruit",
    "3.00" => "kiwi",
    "5.60" => "pineapple",
    "4.20" => "grapes"
    );    
$search = array_search($day, $weekend);
$search1 = array_search($type, $fruit_normal);
$search2 = array_search($type, $fruit_we);
$search3 = array_search($day, $days);    
if($search1!= FALSE && $search3 != FALSE ){
    $price = $search1 * $quantity;
    echo number_format($price, 2); 
}elseif($search != FALSE && $search2 != FALSE){
    $price = $search2 * $quantity;
    echo number_format($price, 2);
}elseif($search == FALSE && $search3 == FALSE){
    echo "error";
}elseif($search1 == FALSE && $search2 == FALSE){
    echo "error";
}

type SomeType = Omit<typeof someObject, "extraField">; typedef可以在打字稿2.8更新日志中找到