Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change in circular type definition results in TypeScript warning #395

Open
jashaj opened this issue Apr 16, 2024 · 7 comments
Open

Change in circular type definition results in TypeScript warning #395

jashaj opened this issue Apr 16, 2024 · 7 comments
Assignees
Labels
bug 🔥 Something isn't working

Comments

@jashaj
Copy link

jashaj commented Apr 16, 2024

Description

After the change in #370, TypeScript sees the key as string | number. This breaks code that expects the key to be a string.

DefinitionList.vue:

<template>
  <div>
    <h2>Record type</h2>
    <dl>
      <definition-item v-for="(value, key) in recordType" :key="key" :term="key" :description="value" />
    </dl>
    <h2>Object type</h2>
    <dl>
      <!-- Error:
           Vue: Type string | number is not assignable to type string
           Type number is not assignable to type string -->
      <definition-item v-for="(value, key) in objectType" :key="key" :term="key" :description="value" />
    </dl>
  </div>
</template>

<script setup lang="ts">
import DefinitionItem from '@/components/DefinitionItem.vue';

const recordType: Record<string, string> = {
  title: 'Text',
  foo: 'bar',
};

const objectType: { [key: string]: string } = {
  title: 'Text',
  foo: 'bar',
};
</script>

DefinitionItem.vue:

<template>
  <div>
    <dt>{{ term }}</dt>
    <dd>{{ description }}</dd>
  </div>
</template>

<script setup lang="ts">
interface Props {
  term: string;
  description: string;
}

defineProps<Props>();
</script>

OpenAPI specification (optional)

No response

Configuration

No response

System information (optional)

No response

@jashaj jashaj added the bug 🔥 Something isn't working label Apr 16, 2024
@jordanshatford
Copy link
Collaborator

@jashaj I have tested this locally in my setup:

Record<string, string> 

# and

{ [key: string]: string }

would act the same (both allowing setting numeric keys x[1] = 'some string'. Are you able to provide a setup for reproduction. They should act the same.

@jordanshatford
Copy link
Collaborator

@jashaj also using Object.keys, it thinks both have string keys. Not sure why with Vue it differs

@mrlubos
Copy link
Member

mrlubos commented Apr 17, 2024

At a quick glance, this seems to be a Vue issue? vuejs/core#8819

@mrlubos
Copy link
Member

mrlubos commented Apr 17, 2024

@jordanshatford remind me why was that type changed?

@jordanshatford
Copy link
Collaborator

@mrlubos #370

@mrlubos
Copy link
Member

mrlubos commented Apr 17, 2024

Should we try to detect circular imports and apply that type only then? Would that minimise the chance of this kind of issues?

@jordanshatford
Copy link
Collaborator

@mrlubos im not sure. In my above comment Record<string, string> DOES allow settings x[1] = 'some string'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🔥 Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants