#Can someone please explain why there's an error here?

12 messages · Page 1 of 1 (latest)

manic vineBOT
#
val#5314

Preview:```ts
type TestA = "Test A"
type TestB = "Test B"

export interface SampleA {
type: "A"
test?: {
sub: TestA
h: string
}[]
}

export interface SampleB {
type: "B"
test?: {
sub: TestB
h: string
}[]
}

export type MyType = SampleA | SampleB
...```

boreal fern
#

Mouse over test and see the type

#

Mouse over test and see the type

#

test is "either SampleA['test'] or SampleB['test']"

#

The value you are trying to push into is allowed only if test is SampleA['test'], which you don't know.

#

Not quite sure what your code's intent is, but I'm guessing your problem is:

// incorrect
declare const foo: string[] | number[]
foo.push('hello')
foo.push(42)

// correct
declare const bar: (string | number)[]
bar.push('hello')
bar.push(42)
#

!ts

manic vineBOT
#
// incorrect
declare const foo: string[] | number[]
foo.push('hello')
//       ^^^^^^^
// Argument of type 'string' is not assignable to parameter of type 'never'.
foo.push(42)
//       ^^
// Argument of type 'number' is not assignable to parameter of type 'never'.

// correct
declare const bar: (string | number)[]
bar.push('hello')
bar.push(42)
barren glade
# boreal fern Not quite sure what your code's intent is, but I'm guessing your problem is: ```...

this is my original code, im getting the same problem on chunks property

export interface IScriptDependency {
   type: "script";
   AST?: Node;
   source: string;
   content: string;
   dependencyMap: IDependencyMap;
   chunks?: {
      AST: Node;
      source: string;
      content: string;
   }[];
}

export interface IStyleDependency {
   type: "style";
   AST?: Root;
   source: string;
   content: string;
   dependencyMap: IDependencyMap;
   chunks?: {
      AST: Root;
      source: string;
      content: string;
   }[];
}

export type IDependency = IScriptDependency | IStyleDependency;
#

what should i do

#

this is so confusing to me sorry