打字稿:将必需的属性转换为可选

时间:2018-09-03 03:47:45

标签: typescript

如何将非可选属性转换为可选属性? 这是代码:

 import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator, MatSort, MatTableDataSource } from '@angular/material';
import { APIService } from '../shared/api.service';
import { Router } from '@angular/router';
import { DataService } from '../shared/data.service';

@Component({
  selector: 'app-reviewerhome',
  templateUrl: './reviewerhome.component.html',
  styleUrls: ['./reviewerhome.component.css']
})
export class ReviewerhomeComponent implements OnInit {
  displayedColumns = ['id', 'fname', 'lname', 'status'];
  dataSource: MatTableDataSource<ApplicantData>;
  datalist:any = [];
  ApplicantIndex: any;

  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;


  constructor(private apiService: APIService, dataService: DataService, private router: Router) {

  }

  ngOnInit() {
   this.apiService.getAllApplicants(this.ApplicantIndex).subscribe(datalist => {
     console.log('Return from getAllApplicants()');
     this.datalist = datalist;


     this.dataSource = new MatTableDataSource(datalist.content);
     console.log(datalist);
     }); 

  }

  ngAfterViewInit() {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;

  }

  applyFilter(filterValue: string) {
    filterValue = filterValue.trim(); // Remove whitespace
    filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
    this.dataSource.filter = filterValue;
  }
}


export interface ApplicantData {
  id: number;
  fname: string;
  lname: string;
  status: string;
}

1 个答案:

答案 0 :(得分:2)

这里只是一个更改,尽管它不适用于问题中的问题。这里的解决方案也保留了键的类型。 在此处阅读更多信息:https://www.typescriptlang.org/docs/handbook/advanced-types.html#index-types

#include <bitset>
#include <cstdio>
#include <cassert>

int main() 
{
    std::bitset<1> aoeu;
    int ret, input;

    ret = std::scanf( "%d", &input );
    assert( ret == 1 );

    aoeu[0] = input;

    std::printf( "%d\n", static_cast<int>(aoeu[0]) );
}