给出一些冻结的对象:
const obj = Object.freeze({a: 1, b:2});
它的类型为Readonly<{a:number, b:number}>
,是否有任何方法可以定义它,使其类型为Readonly<{a:1, b:2}>
,而无需进行强制转换?
我想要这样做的原因是,我想使用值类型来缩小另一个函数的输入范围,例如:function foo(input: 1 | 2)
答案 0 :(得分:1)
function l<T extends number>(p: T ): T {
return p;
}
const c = { a: l(1), b: l(2) };
// const fc: Readonly<{ a: 1; b: 2; }>
const fc = Object.freeze(c);
或
function lc<T extends number>(p: { [k: string]: T }) : { [k: string]: T } {
return p;
}
// const t1: { [k: string]: 1 | 2 | 3; }
const t1 = lc({ a: 1, b: 2, c: 3 });
答案 1 :(得分:0)
如果要保持值的不变性,只需使用冻结的装饰器定义Readonly类即可。
export function frozen(target: Function): void {
Object.freeze(target);
Object.freeze(target.prototype);
}
答案 2 :(得分:0)
在即将发布的TypeScript(3.4)版本中,您可以执行以下操作:
<?php
$con=mysql_connect('localhost', '1093913', 'tanim1996');
$db=mysql_select_db('1093913');
if(isset($_POST['button'])){ //trigger button click
$numRows = 0;
if(!empty($_POST['search'])) {
$search = mysql_real_escape_string($_POST['search']);
$query = mysql_query("select * from iconic19 where student_id like '%{$search}%' || name like '%{$search}%' || phone like '%{$search}%' || blood like '%{$search}%' || district like '%{$search}%' ");
$numRows = (int)mysql_num_rows($query);
}
if ($numRows > 0) {
while ($row = mysql_fetch_array($query)) {
echo "<tbody>";
echo "<tr>";
echo "<td data-label='Student ID'>".$row['student_id']."</td>";
echo "<td data-label='Name' style='font-weight:bold;' >".$row['name']."</td>";
echo "<td data-label='Mobile No'>"."<a href='tel:".$row['phone']."'>".$row['phone']."</a>"."</td>";
echo "<td data-label='Blood' style='color:red; font-weight:bold;' >".$row['blood']."</td>";
echo "<td data-label='Email'>"."<a href='mailto:".$row['email']."'>".$row['email']."</a>"."</td>";
echo "<td data-label='District'>".$row['district']."</td>";
echo "</tr>";
echo "</tbody>";
}
} else {
echo "<div class='error-text'>No results</div><br><br>";
}
} else { //while not in use of search returns all the values
$query = mysql_query("select * from iconic19");
while ($row = mysql_fetch_array($query)) {
}
}
mysql_close();
?>