我正在TypeScript 3.6.2中编译,启用了strictNullChecks。
假设我声明了一个可能未定义的变量:
let filename: string|undefined;
然后,回调可能为其赋值,或将其保留为未定义的值:
doIt(() => filename = "assigned");
现在我检查分配给filename; otherwise,filenameis undefined and I exit the program (return value ofnever`):的回调是否
if (filename === undefined) {
p
我使用的是Typescript 3.7断言签名,它们非常棒!但我也想在异步函数中使用它们,但我得到了一个错误:
Type 'void' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
如果断言签名不能与异步函数一起工作,这将是有意义的,因为Typescript将需要评估Promise是否被解析,但我不确定是否有官方立场来支持它。
我想知道TypeScript中的类型保护,以及当方法签名中只定义了一种类型时是否有必要使用它们。TypeScript文档中的所有示例仅指具有联合类型的情况,例如:
myMethod(argument: string | number) {
if (typeof argument === 'string') { // do my thing }
if (typeof argument === 'number') { // do my thing }
但我见过人们在类型为强类型时使用typeof:
myMethod(argument: string) {
if
当接口中的所有键在类型记录中都是可选的时,所提供的意外字段不是报告错误,为什么?下面是我的代码片段:
// This is an interface which all the key is optional
interface AxiosRequestConfig {
url?: string;
method?: string;
baseURL?: string;
headers?: any;
params?: any;
paramsSerializer?: (params: any) => string;
data?: any;
timeout?: n
目前,我们正在将代码库迁移到typescript,我们有很多地方可以使用任何类型的代码。因此,我尝试强制显式设置变量作为任何变量。
下面是示例代码片段。
const a: string = 'h';
const b: any = 4;
const message = a || b;
// Here I would like to get warning that I am assigning any to the message,
// so I would have to write const message: any = a || b;
function say(x
我有一个函数URLBuilder,它作为一个模块工作(它返回一个包含各种函数的对象,这些函数与上下文对象绑定得很好,所以this绑定按预期工作,我们可以在这些函数中正确地访问this ) 我的问题是为什么typescript不理解URLBuilder的返回对象中函数的this绑定?尽管我尝试手动添加一个返回类型,但Typescript抱怨when using the URLBuilder在没有this上下文的情况下无法接受函数。 我们如何让typescript在返回的对象中理解这个绑定函数的this? 我要解决的Typescript错误是: The 'this' contex
我最近在TypeScript上偶然发现了这种奇怪的行为。在编译期间,只有当期望变量的类型是接口时,如果接口没有强制字段,它才会抱怨过多的属性。链接到TypeScript游乐场#1:
interface IAnimal {
name?: string;
}
class Animal implements IAnimal {
}
var x : IAnimal = { bar: true }; // Object literal may only specify known properties, and 'bar' does not exist in type
我在TypeScript 3.2.2和strict: true上。
我无法键入具有一组动态定义键的普通对象,但所有这些键都有一个有保证的非空值类型。
[TypeScript游乐场示例]()
// Will assume any property access has type T
interface Object<T> {
[key: string]: T;
}
// Will assume Object.values() could be undefined
interface Object<T> {
[key: string]: T | undef
我正在尝试创建一个这样的函数: function assert<T>(condition: T, msg?: string): T & asserts condition {
if (!condition) {
throw new Error(msg)
}
return condition
} 这样做的目的是返回未经修改的condition,保留其类型,但也通知编译器返回值为真。这将允许它作为独立的语句使用,但也可以在表达式中不显眼地使用: const success: bool = true
// ... some code that might s
JSX工厂:
function h(type: string, _props, ..._children): HTMLElement {
return document.createElement(type); // breakpoint here is hit
}
tsconfig.json:
"jsx": "react",
"jsxFactory": "h"
在.tsx文件中的用法:
const element = <div></div>; // = any
TypeScript似乎没有推断出J
我有一个这样的代码: // Those two come from backend, it cannot be guaranted
// they do not contain additional fields of any type.
interface Foo1 {
name: string;
}
interface Foo2 {
description: string;
}
// This one is mine and should always contain valid types
class Bar {
text: string;
con
在TypeScript中,我发现在处理解析后的JSON数据时,有时必须使用“any”类型。不幸的是,这使得忽略类型约束变得很容易,因为TypeScript允许从“any”类型进行隐式转换。
var obj = JSON.parse('{ "num": "this is a string", "str": 3 }');
var num: number = obj.num; // num = "this is a string"!
var str: string = obj.str; // str = 3!
我正
给定以下代码片段
let x = {
name: 'John',
age: 30
}
interface Employee {
name:string,
}
let y:Employee = <Employee>x;
console.log(y); //y still have the age property, why
为什么typescript会忽略这个类型转换,有没有其他的解决方案来只获取这些接口属性?
我想定义一个带参数的函数,它是一个构造函数。在函数中,我想访问构造函数的name属性。是否可以使用TypeScript创建此类型约束?
function doSomethingWithAConstructor(ctor: { new (); name: string }): string {
return ctor.name;
}
更新:
预期用法为:
class A {}
doSomethingWithAConstructor(A);
当我们输入断言时,如果Typescript知道我们不可能是对的,它就会报错。例如 type Counter = (start: number) => number;
let counterProblemDetected = ((start: number) => "yo") as Counter; 但是当非空检查被启用时,它不会像我们设置了类型时那样抱怨没有从函数返回: // 'strictNullChecks' is on
// Typescript does not complain
let counterProblemNotDetected
我已经用typescript和npx create-next-app --example with-typescript with-typescript-app ()开始了一个新的Next.js项目。
无论何时,当我尝试创建这样的类时:
export class Programm {
id: string;
name: string;
thumbnailUrl: string;
}
我得到语法错误:
Property 'id' has no initializer and is not definitely assigned in the constru