英文:
How to correctly initialize vector of number in Typescript
问题
I'm having difficulties at initializing a variable with the value returned from a function.
The function is hosted here and it should return an array of numbers.
So I try to define my variable as
import {default as linspace} from '@stdlib/array-linspace';
var xvalues:number[] = linspace(0,10, 10);
And the IDE gives me an error because Type 'Float64Array' is missing the following properties from type 'number[]': pop, push, concat, shift, and 5 more.ts(2740)
.
So I checked what was actually being returned by the function with the following logs:
console.log(typeof linspace(0,10,10) )
console.log(typeof linspace(0,10,10)[0] )
And the types are in fact
Object
number
Then why defining my variable as var xvalues:number[]
is not correct?
英文:
I'm having difficulties at initializing a variable with the value returned from a function.
The function is hosted here and it should return an array of numbers.
So I try to define my variable as
import {default as linspace} from '@stdlib/array-linspace'
var xvalues:number[] = linspace(0,10, 10)
And the IDE gives me an error because Type 'Float64Array' is missing the following properties from type 'number[]': pop, push, concat, shift, and 5 more.ts(2740)
So I checked what was actually being returned by the function with the following logs:
console.log(typeof linspace(0,10,10) )
console.log(typeof linspace(0,10,10)[0] )
And the types are in fact
Object
number
Then why defining my variable as var xvalues:number[]
is not correct?
答案1
得分: 2
错误消息告诉你要使用 Float64Array
类型:
let xvalues: Float64Array = linspace(0, 10, 10)
(新代码中不应该使用 var
。)
不过,不需要显式注释,TypeScript 会自动推断类型:
let xvalues = linspace(0, 10, 10)
那为什么将我的变量定义为
var xvalues: number[]
不正确?
因为 number[]
和 Float64Array
是不同的东西。虽然 Float64Array
包含 number
值,但它是一个与标准数组不同的容器。值得注意的是,当你从 Float32Array
、Int8Array
或除了 BigInt64Array
之外的任何其他类型化数组中访问值时,都会得到相同的结果,尽管它们包含不同类型的数字。这是因为在访问类型化数组中的值时,它会从原始类型(例如,在 Int8Array
的情况下,是 8 位的二进制补码数)转换为普通的 number
类型。
英文:
The error message is telling you what type to use Float64Array
:
let xvalues: Float64Array = linspace(0,10, 10)
(var
shouldn't be used in new code.)
That said, there's no need for an explicit annotation, TypeScript will infer it for you:
let xvalues = linspace(0,10, 10)
> Then why defining my variable as var xvalues:number[] is not correct?
Because number[]
and Float64Array
are different things. It's true that a Float64Array
contains number
values, but it's a different container than a standard array. It's also worth noting you'd have gotten the same result logging the type of an element value from a Float32Array
, an Int8Array
, or any other typed array other than BigInt64Array
even though the contain different kinds of numbers, because when you access a value in a typed array, it's converted from its raw type (an 8-bit two's complement number, for instance, in the case of an Int8Array
) to the plain number
type.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论