[英]Typescript String literal typing
I have this Typescript type我有这个打字稿类型
export type TCard = {
col: number;
id: number;
hiddenImgPath: "/assets/img/cards/hiddenImg.jpg";
imgPath: "/assets/img/cards";
onClickCard: Function;
row: number;
visible: boolean;
};
prop 'hiddenImgPath' will allways have the value "/assets/img/cards/hiddenImg.jpg". prop 'hiddenImgPath' 将始终具有值“/assets/img/cards/hiddenImg.jpg”。 prop 'imgPath' will always start with "/assets/img/cards" but wil have a suffix (as in '/assets/img/cards/card01.jpg'). prop 'imgPath' 将始终以“/assets/img/cards”开头,但会有后缀(如'/assets/img/cards/card01.jpg')。 Then I create an object that is typed as TCard (elsewhere, not inline):然后我创建一个类型为 TCard 的对象(在其他地方,不是内联):
({
col: i % nrOfColumns,
id: i,
hiddenImgPath: "/assets/img/cards/hiddenImg.jpg",
imgPath: `${imgPath}/fileName`,
onClickCard: onClickCard,
row: Math.floor(i / nrOfColumns),
visible: true
})
But I get an error:但我收到一个错误:
Types of property 'hiddenImgPath' are incompatible.
Type 'string' is not assignable to type '"/assets/img/cards/hiddenImg.jpg"'.
Makes sense so I change the type of 'hiddemImgPath' to有道理,所以我将“hiddemImgPath”的类型更改为
hiddenImgPath: "/assets/img/cards/hiddenImg.jpg" as string;
But then I get an error但后来我得到一个错误
';' expected. TS1005
2 | col: number;
3 | id: number;
> 4 | hiddenImgPath: "/assets/img/cards/hiddenImg.jpg" as string;
| ^
5 | imgPath: "/assets/img/cards";
6 | onClickCard: Function;
7 | row: number;
Why do I have to add a semicolon there?为什么我必须在那里添加分号? And why do I have to add / assign the literally typed property 'hiddenImgPath' in the first place when creating the object of type TCard?为什么我必须在创建 TCard 类型的对象时首先添加/分配字面类型属性“hiddenImgPath”? hiddenImgPath will never have another value than assigned in the type? hiddenImgPath 永远不会有比在类型中分配的另一个值? But if I leave 'hiddenImgPath' out when creating the object of type TCard I get:但是,如果我在创建 TCard 类型的对象时将 'hiddenImgPath' 排除在外,我会得到:
Property 'hiddenImgPath' is missing in type '{ col: number; id: number; imgPath: string; onClickCard: (card: {}) => void; row: number; visible: boolean; }' but required
in type 'TCard'.'
I'm out of ideas.我没有想法了。 Is what I'm trying to do here going against typescript?我在这里尝试做的事情是否与打字稿背道而驰?
Why do I have to add a semicolon there?为什么我必须在那里添加分号?
You don't, but "/assets/img/cards/hiddenImg.jpg" as string
is invalid syntax for a type declaration.你没有,但是"/assets/img/cards/hiddenImg.jpg" as string
对于类型声明来说是无效的语法。 You can't use as
within a type annotation;您不能在类型注释中使用as
; it's for type assertions on values .它用于对values进行类型断言。
The hiddenImgPath
in that object literal is just fine.该对象文字中的hiddenImgPath
很好。 But imgPath
can't be defined with a string literal type if the values will only be prefixed by that string;但是imgPath
不能用字符串文字类型定义,如果这些值只会以该字符串为前缀; you need to use string
for the type:您需要使用string
作为类型:
export type TCard = {
col: number;
id: number;
hiddenImgPath: "/assets/img/cards/hiddenImg.jpg";
imgPath: string; // <===========
onClickCard: Function;
row: number;
visible: boolean;
};
If you do that, your object literal is just fine.如果你这样做,你的对象文字就好了。 Here it is on the playground with some fixed values in place of your variables: playground link这是在操场上用一些固定值代替您的变量: 操场链接
And why do I have to add / assign the literally typed property 'hiddenImgPath' in the first place when creating the object of type TCard?为什么我必须在创建 TCard 类型的对象时首先添加/分配字面类型属性“hiddenImgPath”? hiddenImgPath will never have another value than assigned in the type? hiddenImgPath 永远不会有比在类型中分配的另一个值?
Because TypeScript's work happens at compile-time , not runtime.因为 TypeScript 的工作发生在编译时,而不是运行时。 It won't automatically fill in properties for you, not even when they have only one valid fixed value.它不会自动为您填充属性,即使它们只有一个有效的固定值。 If you want that done once in a central place, you probably want class
rather than just type
.如果您想在一个中心位置完成一次,您可能需要class
而不仅仅是type
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.