在TypeScript中,我遇到了一个奇怪的问题,似乎我应该能够推断出常数是一个数组,并且有了这个知识调用数组的方法。但是,即使经过显式检查,TypeScript显然也无法确定值是否是真正的数组。
给予:
type Maybe<T> = T | null | undefined;
type Value = string
| number
| string[]
| number[]
| Maybe<number | string>[];
如果我在常量上键入常量,则可以调用.every数组方法:
// This approach works fine:
c
突然,我的路线开始告诉我这个错误,我不知道它有什么问题。
我正在尝试从中间件传递id。并使用它来获取文档。
我创建了一个字段名hostId,其中存储了在string表单中创建项目的用户的id。
错误:
CastError: Cast to ObjectId failed for value "proj" (type string) at path "_id" for model "Student"
at model.Query.exec (D:\KabirProject\FindAlly\node_modules\mongoose\lib
我在我的服务器和前端之间共享类型定义。它们在一个单独的npm包中定义,这两个包都会安装。在前端,保存ObjectIds的变量需要这样类型化,但在客户端,我可以假设它们始终是原始字符串。
我在客户端上有很多地方抛出了:
Type 'ObjectId' is not assignable to type 'string'.
处理此错误的最简单方法是什么?我可以告诉Typescript在客户端接受ObjectId的字符串赋值,反之亦然吗?我应该尝试覆盖ObjectId的Mongoose定义吗?
我正在考虑一种重写
declare global {
export
Typescript在这一行抱怨:
user.posts.pull(postId);
我得到了这个错误:
Property 'pull' does not exist on type 'PostDoc[]'
由于postId是作为req.params.postId接收的,所以它是string类型,所以我将其转换为mongoose objectId,但我仍然有相同的错误:
user.posts.pull(mongoose.Types.ObjectId(postId));
pull()在mongoose数组中工作。这行代码是我如何在javacsript
考虑一下这个例子:
class A {
optional?: string;
nonOptional: string;
constructor() {
}
}
let a = new A();
function consumeString(str: string) {
console.log(str.length);
}
consumeString(a.optional);
consumeString(a.nonOptional);
启用strictNullChecks后,TypeScript 2.4.2将拒绝第一次调用;
'strin
假设我正在制作一个函数,它可以返回唯一的参数本身,在javascript中,它将如下所示: function returnItself(x) {
return x;
} 我还想保持参数的类型不变,并使参数成为可选的,所以我写道: function returnItself<T>(x?: T) {
return x;
} 但是结果是..。 var a1 = returnItself("foo");
type A1 = typeof a1; // expect A1 to be "string", but it's "strin
使用Typescript将其转换为NodeJS。所以主要的问题是我正在尝试使用Mongoose遵循一对多的文档结构。但正如问题所说,我面临的问题是:
throw new TypeError(`Invalid schema configuration: \`${name}\` is not ` +
TypeError: Invalid schema configuration: `Todo` is not a valid type at path `ref`
以下是模型代码:
const Schema = mongoose.Schema;
const userSchema = new Sche
我正试图用MERN栈制作一个应用程序。当我试图向当前数据库中添加一个用户时,它会返回CastError: Cast to ObjectId failed for value "undefined" at path "_id" for model "Project",我做错了什么?
API.js
import axios from "axios";
export default {
updateUser: function (id, data) {
return axios.put("/api/user/
我正在将我的项目移植到typescript,而我的模式在一个名为alias的字段上遇到了问题。我得到了这个错误:
Types of property 'alias' are incompatible.
Type 'StringConstructor' is not assignable to type 'string'.ts(2345)
我知道这是因为mongoose有别名字段()。我的问题是,我该如何避免这种情况?
我的代码供参考:
import mongoose, { Schema, Model } from 'mongoose
我正在做一个社交媒体项目,当我发送类似/不像post请求的邮件时,我得到了这个
(E:\social-media-app-mern\node_modules\mongoose\lib\query.js:4639:21) (E:\social-media-app-mern\node_modules\mongoose\lib\query.js:4738:15) at model.Query.Query.then (E:\social-media-app-mern\node_modules\mongoose\lib\query.js:4738:15) at processTicksAndReject
我好像收到了这个错误:
C:\...\node_modules\mongoose\lib\query.js:4638
const castError = new CastError();
^
CastError: Cast to ObjectId failed for value "undefined" (type string) at path "_id" for model "cProd"
at model.Query.exec (C:\...\node_modules\mongoose\
以下是我的错误:
.../mydir/mongoose/node_modules/mongoose/lib/query.js:4719
const castError = new CastError();
^
CastError: Cast to ObjectId failed for value "628601191fc717b2266ce54d " (type string) at path "_id" for model "Product"
at model.Query.exec (/h
以以下TypeScript代码为例:
const a: 0 | 1 = 1
if (a === 0) {
console.log('a is 0')
}
if语句将生成以下错误:
This condition will always return 'false' since the types '1' and '0' have no overlap.
(您可以在TypeScript游乐场中使用下面的示例:)
我的问题是,为什么这会导致错误?如果您悬停在a类型上,则它被正确地键入为const a: 0 | 1。如果编译器如
给定以下代码片段
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会忽略这个类型转换,有没有其他的解决方案来只获取这些接口属性?