created at 2023/08/05 04:30:35
updated at 2023/08/05 04:56:49
typescript
let str = `123`;
str = `456`;
const _constStr = `hello`;
// _constStr = `` //常量赋值错误
let x: `hello` = `hello`;
function logText(str: string, alignItems: 'left' | 'center' | 'right') {}
logText('hello', 'left');
function compare(a: string, b: string): -1 | 0 | 1 {
return a === b ? 0 : a > b ? 1 : -1;
}
interface Options {
width: number;
}
function setWidth(x: Options | 'auto') {}
setWidth({
width: 100,
});
setWidth('auto');
let b1: false = false;
let b2: true = true;
const obj = {
count: 0,
};
if (true) {
obj.count = 1;
}
function req(url: string, method: 'GET' | 'POST' | 'PUT') {}
const opt = {
url: 'https://api.user',
method: 'GET',
};
req(opt.url, opt.method /*as 'GET'*/); // 类型“string”的参数不能赋给类型“'GET'|'POST'|'PUT'”的参数。